By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Is there anyway to pass a js variable in html link?

P: n/a
Hi,

On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should mention
that these will be dynamically created links in php. This is
an optional value so if javascript is disabled, my site will still
function.

the browser_width() function works correctly but when i
call it in the html link, it does not return a value but rather
the funciton call. Is there a way i can do this?

Thanks, Chuck
test.js
------------------------------------------------------
function browser_width()
{ if (document.all)
{ return(document.body.clientWidth);
}
else if (document.getElementById)
{ return(document.width);
}
else
{ return('NULL');
}
}

test.php
--------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>orderbuttons - <? echo $page_name;?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="../javascript/test.js"></></script>
</head>
<body>

<p>
<a
href="http://www.site.com/page2.php?browserwidth=javascript:browser_width()" >test</a>
</p>

</body>
</html>
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"olga" <ol**@eatspm.com> wrote in message
news:2u*************@uni-berlin.de...
Hi,

On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should mention
that these will be dynamically created links in php. This is
an optional value so if javascript is disabled, my site will still
function.

the browser_width() function works correctly but when i
call it in the html link, it does not return a value but rather
the funciton call. Is there a way i can do this?

Thanks, Chuck
test.js
------------------------------------------------------
function browser_width()
{ if (document.all)
{ return(document.body.clientWidth);
}
else if (document.getElementById)
{ return(document.width);
}
else
{ return('NULL');
}
}

test.php
-------------------------------------------------------------------------- ------------ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>orderbuttons - <? echo $page_name;?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="../javascript/test.js"></></script>
</head>
<body>

<p>
<a
href="http://www.site.com/page2.php?browserwidth=javascript:browser_width()"test</a>
</p>

</body>
</html>


Something like this? Watch for word-wrap.

<html>
<head>
<title>orderbuttons</title>
<script type="text/javascript">
function browser_width() {
var wid = 0;
if (document.all) {
wid = document.body.clientWidth;
} else if (document.getElementById) {
wid = document.width;
}
location.href = "http://www.site.com/page2.php?" + wid;
}
</script>
</head>
<body>
<a href="javascript:browser_width()">test</a>
</body>
</html>
Jul 23 '05 #2

P: n/a
McKirahan wrote:
"olga" <ol**@eatspm.com> wrote in message
news:2u*************@uni-berlin.de...
Hi,

On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should mention
that these will be dynamically created links in php. This is
an optional value so if javascript is disabled, my site will still
function.

<snip>
<a href="javascript:browser_width()">test</a>


And if Javascript is disabled? That makes your solution broken, which
the OP was trying to avoid. The only change would be to make it so that
the link linked to the page so that if JS is disabled or not present,
then the site still works. This is covered in the Group FAQ.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

P: n/a
On Tue, 26 Oct 2004 17:07:57 -0500, olga <ol**@eatspm.com> wrote in
message <2u*************@uni-berlin.de> :
Hi,

On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should mention
that these will be dynamically created links in php. This is
an optional value so if javascript is disabled, my site will still
function.

the browser_width() function works correctly but when i
call it in the html link, it does not return a value but rather
the funciton call. Is there a way i can do this?


Sounds just the job for innerHTML.
It's very basic, just to give you an idea.
Some of the lines below may be wrapped.

<p>Click on the
<span id="thephplink">
<a href="link.php?variable=noscript">OLD LINK</a>
</span>
which will read NEW LINK is javascript is enabled.
</p>

function changelink()
{
document.getElementById("thephplink").innerHTML="< a
href='link.php?variable=working'>NEW LINK</a>"
}

--
FZS600 - Silver/Black
GS125 - Black/Rust
Ford 100E Prefect - Black, naturally
Whisky - Aberlour Cask Strength
Jul 23 '05 #4

P: n/a
On Wed, 27 Oct 2004 00:35:04 +0100, PDannyD
<da*****@REMOVETHISBITscenicplace.freeserve.co.u k> wrote:

[snip]
<p>Click on the
<span id="thephplink">
<a href="link.php?variable=noscript">OLD LINK</a>
</span>
which will read NEW LINK is javascript is enabled.
</p>

function changelink()
{
document.getElementById("thephplink").innerHTML="< a
href='link.php?variable=working'>NEW LINK</a>"
}
Or, more simply:

<a href="link.php?variable=noscript" onclick="changeLink(this)"some link</a>


function changeLink(o) {
o.href = o.href.replace('noscript', 'working');
}

That is, modify the link address, rather than the entire link.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
On Tue, 26 Oct 2004 17:07:57 -0500, olga <ol**@eatspm.com> wrote:

[snip]
the browser_width() function works [...]
I doubt that, somehow.
it does not return a value but rather the funciton call. Is there a way
i can do this?
My answer is in response to PDannyD.

[snip]
function browser_width()
{ if (document.all)
{ return(document.body.clientWidth);
}
else if (document.getElementById)
{ return(document.width);
}
else
{ return('NULL');
}
}
As I said in your previous thread, this is nonsense. Support for
document.all has no relationship whatsoever with the ability to determine
the width of the browser using clientWidth.

Netscape 7 and all versions of Mozilla return the client width using the
document.all path, but none of them will evaluate docuemnt.all as true.
Similarly, Opera supports document.getElementById, but it doesn't support
document.width.

For information on why browser detection, in all its forms, fails and what
do to instead, see
<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>.

See the link I gave in the previous thread for a proper solution to obtain
browser dimensions. Of course, it appears that you're trying to get the
server to produce different pages based on viewport size. Why don't you
just write a page using a fluid layout, thereby avoiding this issue
altogether?

[snip]
<script src="../javascript/test.js"></></script>


SCRIPT elements require a type attribute. Also, SCRIPT elements that use
the src attribute should have no content.

<script type="text/javascript"
src="../javascript/test.js"></script>

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

P: n/a
"Randy Webb" <Hi************@aol.com> wrote in message
news:Nv********************@comcast.com...
McKirahan wrote:
"olga" <ol**@eatspm.com> wrote in message
news:2u*************@uni-berlin.de...
Hi,

On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should mention
that these will be dynamically created links in php. This is
an optional value so if javascript is disabled, my site will still
function.


<snip>
<a href="javascript:browser_width()">test</a>


And if Javascript is disabled? That makes your solution broken, which
the OP was trying to avoid. The only change would be to make it so that
the link linked to the page so that if JS is disabled or not present,
then the site still works. This is covered in the Group FAQ.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq


"i want to pass a javascript variable ..." implies that JavaScript is not
disabled!
Jul 23 '05 #7

P: n/a
On Wed, 27 Oct 2004 10:55:26 GMT, McKirahan <Ne**@McKirahan.com> wrote:

[snip]

"olga" <ol**@eatspm.com> wrote in message
news:2u*************@uni-berlin.de...
This is an optional value so if javascript is disabled, my site will
still function.

[snip]
"i want to pass a javascript variable ..." implies that JavaScript is not
disabled!


But the quote I left above clearly implies the OP what's the page to cope
without Javascript.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.