PDA

View Full Version : assigning javascript values



mhodgson
11-21-2007, 01:16 PM
Simple question that I can't seem to find the answer to.

Can I assign a Javascript generated value to url link.
onclick='javascriptvalue.value=1;'
different onclick generate different values

e.g.

<a href="whateverpage.php?something=javascriptvalue">

with javascriptvalue being whatever the the onclick assigns

Thanks

jscheuer1
11-21-2007, 02:28 PM
You could be more specific, here's one way to do something like that:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function aquery(l, n, v){
var a = document.getElementById(l);
a.href = a.href.replace(/\?.*/, '') + '?' + escape(n) + '=' + escape(v);
}
</script>
</head>
<body>
<div>
<a href="javascript:void(0);" onclick="aquery('main', 'var2', '32');return false;">Link Text or Image</a><br>
<a id="main" href="some.php?var1=16">Link Text or Image</a>
</div>
</body>
</html>

Twey
11-21-2007, 02:54 PM
<a href="javascript:'whateverpage.php?something=' + (5 + 9)">

mhodgson
11-21-2007, 04:44 PM
<a href="javascript:'whateverpage.php?something=' + (5 + 9)">

This would seem to be perfect but instead of going to 'whatever.php?something=14' it simply reloads the current page and writes the new url on it.

jscheuer1
11-21-2007, 05:10 PM
Nothing:


<a href="javascript:

Is a good idea if the href ever gets executed, unless the object at that point is at least in part to unload the page.

But that's not the main problem, as that sort of syntax is usually OK. The main problem is that it assigns nothing to anything, and just executes.

Did you try out my idea?

Twey
11-22-2007, 03:23 AM
This would seem to be perfect but instead of going to 'whatever.php?something=14' it simply reloads the current page and writes the new url on it.Really? It should go to whatever.php?something=14. Demo page?
Is a good idea if the href ever gets executed, unless the object at that point is at least in part to unload the page.This is a proper link we're talking about here, which unloads the page and loads a new one. This is what the javascript: pseudo-scheme was designed for.
The main problem is that it assigns nothing to anything, and just executes.I think you misunderstood the original question: mhodgson just wants to go to an URL that has part of itself generated by Javascript. There's no assignment involved.

mhodgson
11-22-2007, 08:46 AM
Really? It should go to whatever.php?something=14. Demo page?This is a proper link we're talking about here, which unloads the page and loads a new one. This is what the javascript: pseudo-scheme was designed for.I think you misunderstood the original question: mhodgson just wants to go to an URL that has part of itself generated by Javascript. There's no assignment involved.

Yep, tried it again. Even started with a clean page and new page just to see if something was messing it up. It still reloads the same page with the link writen on it and does not go to the link page.

jscheuer1
11-22-2007, 09:05 AM
Have you tried my idea yet?

Anyways, to do it like Twey says, you would need to tell it to do something:


<a href="javascript:location='whateverpage.php?something=' + (5 + 9)">

mhodgson
11-22-2007, 10:14 AM
Have you tried my idea yet?

Anyways, to do it like Twey says, you would need to tell it to do something:


<a href="javascript:location='whateverpage.php?something=' + (5 + 9)">

Now that works as I wanted.

Yes I have tried your idea now. It works well on a test page but the onclick event doesn't work on on my page. It could be because other things are triggered with the same onclick or that it is in a table cell with no <a href=......>.

<td class='point widthGROUP' onclick='GoTop(); sortTable(this,1); HoverRow();' title='Sort' align='center'>
No matter how I tried I couldn't get it to fit in and work.

Anyway, thanks for the help, both, it works as I wanted now.