Forum Moderators: open

Message Too Old, No Replies

dynamically modifying links using javascript/ passing variables

         

dave1236

1:03 pm on Sep 1, 2004 (gmt 0)

10+ Year Member



All:

Any and all advice is welcome. I am trying to solve the problem of how to dynamically change a link based on a users click. Quite simply, i am trying to implement commission junctions shopper ID function in my site. Currently, i copy, paste, and edit the identifying portion of the link by hand. This gets pretty tedious after doing the same thing 20 or 30 times.
Specifically, this is what i need to do...i am told it can be done without a database, and with a simple javascript...

Home page links:
A
B
C

Clicking on "A" will bring a user to a new page. I am seeking the way to assign a value to "A" (maybe site 001), and write that value to the following location in the linked page...(where it says "APPEND INFO HERE")
<a href="http://www.qksrv.net/click-****-xxxx?SID=APPEND INFO HERE" target="_blank" >Link</a>
<img src="http://www.qksrv.net/image-xxx-xxx” width="1" height="1" border="0"><br>

If a user clicks on "b" or "c", the same page would be loaded, except that the?SID= field would reflect whatever value I assign to "b" and "c"

Thanks

Cochrane

5:46 pm on Sep 3, 2004 (gmt 0)

10+ Year Member



Dave:
I don't know if this will meet your needs, but I believe it does what you requested. The gist of it is that I open a window, and use "A", "B" or "C" as a pseudo window.name Then in the new window, I use an array to associate the window.name to a string containing the appropriate link. Then the link string is written to the assigned Div.

---------- Main document ------------
<HTML>
<Head>
<Script Language=JavaScript>

function nextPage(link){

window.open("newPage.html",link);
window.opener = "";
window.close();
}

</Script>
</Head>
<Body>
<center>
<a href=javascript:nextPage('A')> A Link </a>
<br><br>
<a href=javascript:nextPage('B')> B Link </a>
<br><br>
<a href=javascript:nextPage('C')> C Link </a>
</center>
</Body>
</HTML>

------------newPage.html-----------
<HTML>
<Head>
<Script Language=JavaScript>

var currLink = window.name;

linkURL = new Array();
linkURL[0] = "/A_Link.html"
linkURL[1] = "/B_link.html"
linkURL[2] = "/C_Link.html"

if (currLink == 'A'){currLink = linkURL[0]}
if (currLink == 'B'){currLink = linkURL[1]}
if (currLink == 'C'){currLink = linkURL[2]}

var linkStr = "<a href=http://www.qksrv.net/click"+currLink+" target='_blank'>Link</a>"

</Script>
</Head>
<Body>
<Div id=isLink><Script>document.write(linkStr)</Script></Div>
</Body>
</HTML>

dave1236

1:57 am on Sep 4, 2004 (gmt 0)

10+ Year Member



Cochrane:

I tried the solution you gave me, but I encountered errors in implementing it. I got an error saying the variable was not defined.

I actually think you have a solution that will work for me. The only further questions i have is:
1) do i just add more "linkURL"s for more links?
2) if i need the +currLink+ to populate over a multitude of other links (ie more qksv.net links) do i just copy, paste, and modify for a multitude of links.

Cochrane

11:31 am on Sep 4, 2004 (gmt 0)

10+ Year Member



Dave:
I don't know what the error could be that you are experiencing. The code worked well for me. I simply thought, after I posted it, that you would want to resize the new window to full-screen.
But yes, you can continue to use "+currLink+", repeatedly. And yes, just add more elements to the array of link URLs. Anyway, if you say this will do it for you, fine. I'm glad I could help. But if you can't find the source of that error, let me know, maybe I can help.

Cochrane

11:33 am on Sep 4, 2004 (gmt 0)

10+ Year Member



Dave:
Oh, and here's what I did to resize the window:

<HTML>
<Head>
<Script Language=JavaScript>

isWidth = screen.availWidth;
isHeight = screen.availHeight;

window.resizeTo(isWidth,isHeight);
window.moveTo(0,0);

var currLink = window.name;

linkURL = new Array();
linkURL[0] = "/A_Link.html"
linkURL[1] = "/B_link.html"
linkURL[2] = "/C_Link.html"

if (currLink == 'A'){currLink = linkURL[0]}
if (currLink == 'B'){currLink = linkURL[1]}
if (currLink == 'C'){currLink = linkURL[2]}

var linkStr = "<a href=http://www.qksrv.net/click"+currLink+" target='_blank'>Link</a>"

</Script>
</Head>
<Body>
<Div id=isLink><Script>document.write(linkStr)</Script></Div>
</Body>
</HTML>

Cochrane

1:32 pm on Sep 4, 2004 (gmt 0)

10+ Year Member



Dave:
You know, I can't see the forest for the trees sometimes. You can do it this way, too:

<a href="http://www.qksrv.net/some.html" target="_blank" NAME='A'>Link</a>
<a href="http://www.qksrv.net/some.html" target="_blank" NAME='B'>Link</a>
<a href="http://www.qksrv.net/some.html" target="_blank" NAME='C'>Link</a>

dave1236

1:11 am on Sep 6, 2004 (gmt 0)

10+ Year Member



Cochrane:

The original code seems to work. I was having trouble publishing the code, hence my problems. If I can bother you once more, the final issue I have is how do I make this work with a drop down menu. i.e. I have the "A" "B" and "C" links within a drop down menu, and I want this code to work when a user selects one of the menu items.
I currently have it set up as a form with OnChange="location=getSelect(this)"

Thanks!

dave1236

1:14 am on Sep 6, 2004 (gmt 0)

10+ Year Member



Cochrane:

and the form uses <OPTION value="http://www.mysite">A

as an example of the form

dave1236

4:22 am on Sep 6, 2004 (gmt 0)

10+ Year Member



my javascript for the form is:

<SCRIPT>
function getSelect(s) {
return s.options[s.selectedIndex].value
}
</SCRIPT>

Cochrane

11:56 am on Sep 6, 2004 (gmt 0)

10+ Year Member



Dave:

<HTML>
<Head>
<Script Language=JavaScript>

function nextPage(isList){

link = isList.options[isList.selectedIndex].value;
window.open("newPage.html",link);

}

</Script>
</Head>
<Body>
<center>
<br><br>
<select onChange="nextPage(this)">
<option selected> Make a selection </option>
<option value='A'> Steel </option>
<option value='B'> Iron </option>
<option value='C'> Lead </option>
</select>
</center>
</Body>
</HTML>