buiding a link in javascript

S

soni2926

hi,
i'm trying to build a link on the fly and add it to the innerHTML
property of a div like so:


var linkstr = '<a onclick=window.location="../mysite.php?id=" +
result.GID + ";">Click Here</a>';
linkToSet.innerHTML = linkstr;

problem is i can get the linkstr to build properly as a link with a
parameter being passed in, the result.GID is a local javascript
variable and has a value. can somsone help and show me how to get
this to build properly?

thank you!
 
K

Kiran Makam

var linkstr = '<a onclick=window.location="../mysite.php?id=" +

Instead of onclick you can use href attribute:

<script>
var linkstr = '<a href="../mysite.php?id='+result.GID+'">Click Here</
a>';
linkToSet.innerHTML = linkstr;
</script>

To load a page using window.location, you will have to use:
window.location.href = "myPage.html";

- Kiran Makam
 
H

Henry

hi,
i'm trying to build a link on the fly and add it to the innerHTML
property of a div like so:

var linkstr = '<a onclick=window.location="../mysite.php?id=" + ^
result.GID + ";">Click Here</a>'; ^ ^ ^
linkToSet.innerHTML = linkstr;

(Assuming that there are no line breaks in the real code) Your string
delimiters are confused. Because your first string literal delimiter
is an apostrophe the literal is not terminated until the next (non-
escaped) apostrophe is encountered. This second apostrophe follows the
"</a>" at the end, so the - + result.GID + - part is a sequence of
literal characters within a string literal and not a concatenation
operation that inserts an object property's value into a mark-up
fragment.

However, what you are doing is mad; using an onclick handler to assign
a URL to the - location - object of the current window. The URL should
be the HREF of a link, and if you don't want the normal link styling
then use CSS to suggest the styling you do want.
 
S

soni2926

Instead of onclick you can use href attribute:

<script>
var linkstr = '<a href="../mysite.php?id='+result.GID+'">Click Here</
a>';
linkToSet.innerHTML = linkstr;
</script>

To load a page using window.location, you will have to use:
window.location.href = "myPage.html";

- Kiran Makam

Thank you! Not sure what i was thinking not using href instead...
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top