0

I am trying to execute a js action that automatically sends informations to the URL for future use with php. To do so, I added a "onload" event on the window object and modified the URL in the listener. This is what it looks like so far:

window.addEventListener("load", function() {
    window.location = "?test=test";
}, false);

When I load the page, the URL changes, but this is repeated over and over until the browser crashes. I was just wondering if there was a way to only execute it once.

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
  • `sends informations to the url for future use with php.` What are you trying to achieve exactly? I'm positive there's another, better, way than keep redirecting on the same page – Damien Pirsy Dec 30 '13 at 19:21
  • not sure you can, you will have to add a check to see if you have set a url query etc and if not then reload else leave eg check that you have a 'test' value etc in your query etc but its not safe etc, if you want to explain a little more on what your trying to achieve might be easier to help – Simon Davies Dec 30 '13 at 19:21
  • Don't you think this handler will go in an infinite loop. The moment window.onload executes, you are reloading the page, which in turn will call window.onload. – Anup Vasudeva Dec 30 '13 at 19:25
  • but how can i stop the page from reloading again and again? that's what i'm trying to explain... –  Dec 30 '13 at 19:28
  • @DamienPirsy I want to transfer informations from JS to PHP by using URL sends and $_GET, then read a text file in PHP with these informations just sent, then send it back to JS and use it for other stuff. Of course, if you have a better way, it would be great if you told me, because I don't think this is the best way of doing it... –  Dec 30 '13 at 19:34
  • What about using AJAX? – Damien Pirsy Dec 30 '13 at 19:35
  • I tried it once, didn't work very well. Maybe I did something wrong. –  Dec 30 '13 at 19:36
  • @magqudi see my answer, will it fit for you? – Damien Pirsy Dec 30 '13 at 19:43
  • Let me check first, but just by looking, I think it'll work. –  Dec 30 '13 at 19:46

6 Answers6

0

Check to see if you're already ON that page, before redirecting.

Jessica
  • 7,075
  • 28
  • 39
0

Add a simple flag on the URL like so: window.location = "?test=test&second=true";

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
0

As suggested in How can I get query string values in JavaScript?

Write a function to check get the URL params in JS GetQueryStringParams()

Then as suggested by others pass a send param in while reload

window.location = "?test=test&second=true";

Use the function in JS to check if you have a URL query string second and if its not there then reload the page

Community
  • 1
  • 1
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

If you don't want to use AJAX, and don't want to write JS functions to parse query strings, why not a simple:

<?php if(!isset($_GET['test'])): ?>
<script>
   window.addEventListener("load", function() {
       window.location = "?test=test";
   }, false);
</script>
<?php else: ?>

 <!--No JS written on the page, so no redirect -->

<?php endif;?>

But you really should look into AJAX, try using some JS framework like jQuery to help you in the process: http://api.jquery.com/jquery.ajax/

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
0

if you want to pass variables do this:

<?php
    if(!isset($_GET['page'])){ //this line check if the variable field is available 
    $a = "Blank";
    }else{
    $a = $_GET['page'];
    }
?>

that is it.

0

You could just erase the onLoad method once it's run, like so:

<script>
function doLoadStuff()
{
    doSomeStuff();

    //Clear onLoad contents
    body.onLoad = function() {};
}
</script>
<body onLoad="doLoadStuff();">MyContent</body>
rmobis
  • 26,129
  • 8
  • 64
  • 65