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

variables in url

100+
P: 347
i am trying to pass the contents of a javascript variable in a url have tried some ideas but cant get it to work, here is the code i am trying to get the url to appear on the page

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.                           document.write('<a href="boys.asp?id=all' + select_array + '">')
  3.                           </script>link</a>
can anyone help??
Feb 6 '09 #1
Share this Question
Share on Google+
13 Replies


acoder
Expert Mod 15k+
P: 16,027
What's the value of select_array?
Feb 7 '09 #2

100+
P: 347
the select_array has a numeric value upto three numbers long
Feb 7 '09 #3

acoder
Expert Mod 15k+
P: 16,027
When do you include this script? Is it after the page has loaded or during page load?
Feb 7 '09 #4

100+
P: 347
this is after the page load so i can include the varible array in the url to the next page
Feb 7 '09 #5

acoder
Expert Mod 15k+
P: 16,027
After page load, document.write re-opens the page for writing overwriting older content, or at least the result is unexpected.

Use DOM methods, e.g. createElement, appendChild, or set the innerHTML property of an element instead.
Feb 9 '09 #6

100+
P: 347
Hi thanks for the advice i have looked up the createElement and appendChild elements and i think they will do what i want, i just cant figure out how to code them to get the link as i want it

would it be like this

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function function1(){
  3.    var myElement = document.createElement('<A href="boys?id=all');
  4.    myhref.appendChild(myElement);
  5.  
  6.    myElement = document.createElement('&select_array"');
  7.    myhref.appendChild(myElement);
  8.  
  9.    myElement = document.createElement('linktext</a>');
  10.    myhref.appendChild(myElement);
  11. }
  12. </script>
  13. <body>
  14. <button onClick="function1();">link text</button>
  15. </body>
  16. </html>
if not could anyone help on the syntax
Feb 9 '09 #7

acoder
Expert Mod 15k+
P: 16,027
myhref would have to be a valid reference. You will also need to set the attributes, e.g.
Expand|Select|Wrap|Line Numbers
  1. var myElement = document.createElement('A');
  2. myElement.href="boys?id=all&" + select_array;
  3. text = document.createTextNode("linktext");
  4. myElement.appendChild(text);
  5. myhref.appendChild(myElement);
Feb 9 '09 #8

100+
P: 347
i have tried the above but it comes back saying select_array is undefined, also is there a way of setting an image instead of the link text....thanks
Feb 9 '09 #9

acoder
Expert Mod 15k+
P: 16,027
I assumed that select_array was set somewhere.

You can set an image by creating an IMG element using createElement(). Remember to set its src property to the image source.
Feb 9 '09 #10

100+
P: 347
yes the select_array element is set in another function

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function add()
  3. {
  4. var select_array = new Array();
  5. select_array.push(<%=fav%>);
  6. select_array.push(<%=actorid%>);
  7. alert('You have added <%=yaketyRecordset("firstname")%>' + ' ' + '<%=yaketyRecordset("surname")%> to you casting list');
  8. }
  9. function boys()
  10. {
  11. var myElement = document.createElement('A');
  12. myElement.href="boys?id=all&" + select_array;
  13. text = document.createTextNode("linktext");
  14. myElement.appendChild(text);
  15. myhref.appendChild(myElement);
  16. }
  17. </script>
Feb 9 '09 #11

acoder
Expert Mod 15k+
P: 16,027
Either make select_array into a global or return select_array in a function, so that it can be used elsewhere.

Also note that myhref will need to be a valid reference. You could use document.body in place of myhref if you just want it appended to the body.
Feb 9 '09 #12

100+
P: 347
when you say return select_array in a function can i just put return select_array at the beginning of the function tp get it in??? not sure what you mean by global!
Feb 9 '09 #13

acoder
Expert Mod 15k+
P: 16,027
See Variables for an explanation. Strictly speaking, they're properties of the window object. Normally, you'd avoid global variables, but in a simple page (especially where you're fairly new to JavaScript), it shouldn't be too much of a problem.
Feb 9 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.