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

O

olga

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

McKirahan

olga said:
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>
 
R

Randy Webb

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

PDannyD

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>"
}
 
M

Michael Winter

On Wed, 27 Oct 2004 00:35:04 +0100, PDannyD

[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
 
M

Michael Winter

[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
 
M

McKirahan

Randy Webb said:
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.

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

Michael Winter

[snip]

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top