Jump to content

Passing Variable from URL to JavaScript


AndrewM16921

Recommended Posts

I have URLs that look something like this:

 

http://site.com/phpBB3/viewforum.php?f=15

http://site.com/phpBB3/viewtopic.php?f=15&t=128

 

In the forum's Overall_Header.html I have a portion of HTML that I want displayed in all sections except for one, which is f=15.

 

So, what I wanted to do what something like this:

 

if(f!=15)

{

...

}

 

But, i don't know how to grab the value of f from the URL to use in a script. So.. How can I?  :P

Link to comment
https://forums.phpfreaks.com/topic/141010-passing-variable-from-url-to-javascript/
Share on other sites

You could use document.URL and indexOf and some other functions to manipulate it out.

 

 

 

indexOf and substring should be good enough.....

 

var url = document.URL;

var start = url.indexOf('f=');

var f;

if(start >= 0) {

    var end = url.indexOf('&');

    if(end < 0) end = url.length();

    url.substring(url.indexOf('f='), end);

}

 

 

That should work.

as the example links u r providing is php page, i assume u have php supported server. Then do u mind if u use a lil bit of php coding? If not, then u can use the following

<SCRIPT language="javascript">
function myfunction(){
        var f='<?php echo $_GET['f'];?>';
}
</SCRIPT>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.