two javascript questions

L

LaughOutLoud

Hi. Does anyone knows that how to retrieve variable on a link from
javascript.
e.g., here I have a link

<a href="http://www.domain.com/index.html?id=1111"
onClick="getid();">click me</a>
<div id="display"></div>

When I click the link, getid function will retrieve the id value from
the link which is "1111" and display it within <div> tag by using
innerHTML. No page refresh.

Is it possible to do that? If so, does anyone know how to do it?


Thanks
 
B

Brian D

Not exactly sure how you that but why don't you build your link as
follows:

<a href="javascript:void(0)" onClick="getid(1111);">click me</a>
<div id="display"></div>

Brian
 
L

Laurent Bugnion

Hi,

Brian said:
Not exactly sure how you that but why don't you build your link as
follows:

<a href="javascript:void(0)" onClick="getid(1111);">click me</a>
<div id="display"></div>

Brian

Because that's bad programming. If JavaScript is disabled, it will fail.
Additionally, you should never use the javascript: pseudo protocol in a
href.

http://www.jibbering.com/faq/#FAQ4_24

Greetings,
Laurent
 
L

Laurent Bugnion

Hi,
Hi. Does anyone knows that how to retrieve variable on a link from
javascript.
e.g., here I have a link

<a href="http://www.domain.com/index.html?id=1111"
onClick="getid();">click me</a>
<div id="display"></div>

When I click the link, getid function will retrieve the id value from
the link which is "1111" and display it within <div> tag by using
innerHTML. No page refresh.

Is it possible to do that? If so, does anyone know how to do it?


Thanks

Modify getid() to getid( this );

When the method is called, the "A" node is passed to the function, and
you can access its "href" property, which carries the information you
want to display.

"return false;" in the onclick prevents the link to be followed if
JavaScript is enabled (you said you don't want a postback).

<a href="http://www.domain.com/index.html?id=1111"
onclick="getid( this );return false;">click me</a>
<div id="display">&nbsp;</div>

with

function getid( nLink )
{
if ( nLink
&& nLink.href != null
&& nLink.href.indexOf( "=" ) > -1 )
{
var strId = nLink.href.split( "=" )[1];
var nDisplay = document.getElementById( "display" );
if ( display )
{
display.firstChild.nodeValue = strId;
}
}
}

HTH,
Laurent
 
R

Randy Webb

Brian D said the following on 9/27/2006 9:28 AM:
Not exactly sure how you that but why don't you build your link as
follows:

Because that is a stupid way to use a link?
 
R

Randy Webb

Laurent Bugnion said the following on 9/27/2006 10:15 AM:
Hi,
Hi. Does anyone knows that how to retrieve variable on a link from
javascript.
e.g., here I have a link

<a href="http://www.domain.com/index.html?id=1111"
onClick="getid();">click me</a>
<div id="display"></div>

When I click the link, getid function will retrieve the id value from
the link which is "1111" and display it within <div> tag by using
innerHTML. No page refresh.

Is it possible to do that? If so, does anyone know how to do it?


Thanks

Modify getid() to getid( this );

When the method is called, the "A" node is passed to the function, and
you can access its "href" property, which carries the information you
want to display.

"return false;" in the onclick prevents the link to be followed if
JavaScript is enabled (you said you don't want a postback).

<a href="http://www.domain.com/index.html?id=1111"
onclick="getid( this );return false;">click me</a>
<div id="display">&nbsp;</div>

with

function getid( nLink )
{
if ( nLink
&& nLink.href != null
&& nLink.href.indexOf( "=" ) > -1 )
{
var strId = nLink.href.split( "=" )[1];
var nDisplay = document.getElementById( "display" );
if ( display )
{
display.firstChild.nodeValue = strId;
}
}
}

Possibly simpler:

function getid(idToCheck){
document.getElementById( "display" ).innerHTML = idToCheck;
}

<a href="http://www.domain.com/index.html?id=1111"
onClick="getid(this.href.substring(this.href.lastIndexOf('=')+1));
return false">click me</a>
<div id="display"></div>

No, it doesn't have the built in tests, but if the programmer is
programming the page then they should know whether the id is present or not.
 
L

Laurent Bugnion

Hi,

Randy said:
Possibly simpler:

function getid(idToCheck){
document.getElementById( "display" ).innerHTML = idToCheck;
}

<a href="http://www.domain.com/index.html?id=1111"
onClick="getid(this.href.substring(this.href.lastIndexOf('=')+1));
return false">click me</a>
<div id="display"></div>

No, it doesn't have the built in tests, but if the programmer is
programming the page then they should know whether the id is present or
not.

I am a maniac, I know ;-)

Laurent
 
L

LaughOutLoud

What I wanna do is try to put session id into a href. When user click
on the link, javascript can read the session id and pass it to server
and check the session is still available or not. If not, redirect to
index page.

Thanks for the script, I'll test it now.
 

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,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top