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.

Community
  • 1
  • 1
Elemen7s
  • 3
  • 1
  • and where's the javascript in that? – Gabi Purcaru Jun 14 '11 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 '11 at 18:08

6 Answers6

1

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

How to get querystring in JavaScript: JavaScript query string

Community
  • 1
  • 1
Jim
  • 1,695
  • 2
  • 23
  • 42
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
}
John Kalberer
  • 5,690
  • 1
  • 23
  • 27
1

You could do:

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

if (func && typeof(func) == "function")
    func();
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
1
<script language="javascript">
var k = document.location.toString().split('?');

if(k.length > 1)
 {
   if(k[1] == "AC")
      AC();
}
</script>
Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
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.

Wicked Coder
  • 1,128
  • 6
  • 8
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>
mrk
  • 4,999
  • 3
  • 27
  • 42