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
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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.