# OnLoadWebDocument even running only once not after refreshing the page?

**URL:** <https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022>\
**Category:** Random\
**Created:** [September 2, 2023, 11:25pm UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022 "2023-09-02T23:25:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [September 2, 2023, 11:25pm UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/1 "2023-09-02T23:25:55Z")

</div>

Hello I still having issues with the 🤬 cache headers, just one quick question hope somebody can guide me in case I’m wrong.

My problem is simple:

I’m adding a header in my html, a simple Last-Modified: DATE-HERE.

plugin OnLoadWebDocument

code:

```auto
<?php
$id = $modx->resource->get('id');
$page = $modx->getObject('modResource', $id);
$editedon = $page->get('editedon');
$strtotime = strtotime($editedon);
$editedonDate = gmdate("D, d M Y H:i:s", $strtotime) . " GMT";

header("Last-Modified: $editedonDate");

```

I tested in an incognito window it works fine, I can see it on developer tools/network BUT  
if I press F5 and refresh the page, it’s gone!

**WHY???** 🤯

Is there a system event that is executed when the page refresh or something like that?  
I mean to make the code persistent and not just go away to the underworld?  
Am I doing something wrong?

Thank you very much. 👍

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [September 3, 2023, 5:46am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/2 "2023-09-03T05:46:43Z")

</div>

After you add them, the headers are part of the original request, but reloading the page is a new request. You might look at `OnHandleRequest`.

BTW, why not this instead of your first four lines?

```auto
$editedon = $modx->resource->get('editedon');

```

---

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [September 4, 2023, 10:08am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/3 "2023-09-04T10:08:42Z")

</div>

> [@bobray](#):
>
> OnHandleRequest

@bobray mate thanks for your help. 2 things:

1. `OnHandleRequest` didn’t work to set the headers on refresh
2. You are 100% right regarding `$editedon = $modx->resource->get('editedon');` but used with `OnHandleRequest` brakes the whole site including the manager.

Any ideas?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [September 4, 2023, 10:50am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/4 "2023-09-04T10:50:23Z")

</div>

> [@ysanmiguel](#):
>
> but used with `OnHandleRequest` brakes the whole site including the manager.

The event “OnHandleRequest” is invoked before the MODX code determined which resource to load for this request. Therefore `$modx->resource` is not yet available in this event.

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [September 4, 2023, 6:15pm UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/5 "2023-09-04T18:15:01Z")

</div>

I have no experience messing with headers. If your goal is to convince the browser the page has been modified, is there any way you can add the editedon date to the URL as part of a query string?

```auto
https://mypage.com? &ed=01/03/2023

```

---

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [September 7, 2023, 10:14am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/6 "2023-09-07T10:14:41Z")

</div>

That is what I thought @halftrainedharry , thanks mate!

But I just have one **big doubt** and I hope you can help me with this.

If you se my first post I’m running the headers on `OnLoadWebDocument`  
That is working fine only on the first time the website is loaded, if you press F5 to refresh the headers from the plugin just disappear (I’m caching everything but the HTML file).

BUT…

If I’m logged into the manager the headers are there all the time even after refreshing the page, so if I test them everything looks fine but once the site is testes on an incognito window or after log out from the manager no more headers after refresh.

So how can I make the headers Persistent if I’m not logged in the manager?

Thanks!

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [September 8, 2023, 7:11am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/7 "2023-09-08T07:11:55Z")

</div>

I can’t reproduce this.

I created the plugin with the code you posted above and tested it.  
The header is always present, no matter if I’m logged in, logged out or use a private browser window.

* * *

The event `OnLoadWebDocument` should run on every (frontend) request.  
If you’re logged in or not shouldn’t make a difference.

---

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [September 8, 2023, 9:22am UTC](https://community.modx.com/t/onloadwebdocument-even-running-only-once-not-after-refreshing-the-page/7022/8 "2023-09-08T09:22:04Z")

</div>

Thanks @halftrainedharry  
I’ll check what’s going on here, then.

Appreciate your help mate, as always!  
Cheers!
