0

Possible Duplicate:
Running a function by typing in URL

On my website I have several functions that when a hyperlink is clicked on example.php page, the main content changes accordingly. Now I want to direct people from a single URL to that example.php page with the function already called. Example - www.example.com/example.php?AC ( which will call a function named AC and the content changes before the page loads)

I had already asked the same question, but forgot to tag javascript and tagged php.

Thanks in advanced.

2
  • and where's the javascript in that? Jun 14, 2011 at 18:06
  • I want the method to be in Javascript, as in the other question I asked, they gave me a method in php and I couldnt call a javascript function from php( or it is a little complicated)
    – Elemen7s
    Jun 14, 2011 at 18:08

6 Answers 6

1

You can use JavaScript to get the querystring, and call the functions accordingly.

How to get querystring in JavaScript: JavaScript query string

1

What I would do is read the query string by using something like this to figure out if the key is there

function doesKeyExist(key) {
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  return qs != null;
}

Then

if(doesKeyExist(AC)) {
    // run my code
}
1

You could do:

var query = document.location.search;
var func = window[query];

if (func && typeof(func) == "function")
    func();
1
<script language="javascript">
var k = document.location.toString().split('?');

if(k.length > 1)
 {
   if(k[1] == "AC")
      AC();
}
</script>
0

You can add a window.onload handler, check the query string of the page and act accordingly. For example:

<html>
<body>
<script>
function handleOnload()
{
    if(location.search == "?AC")
       alert("the query string is " + location.search);
}

window.onload=handleOnload;
</script>
</body>
</html>
0
0

You run a JS funciton like this.

<script type="text/javascript">

    function test()
    {
        alert('test');
    }

</script>

Now if you type javascript:test() in the url it will execute test function.

Not the answer you're looking for? Browse other questions tagged or ask your own question.