473,626 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

two javascript questions

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=1 111"
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 <divtag by using
innerHTML. No page refresh.

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

Sep 27 '06 #1
7 1849
Not exactly sure how you that but why don't you build your link as
follows:

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

Brian

Sep 27 '06 #2
Hi,

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

<a href="javascrip t: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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 27 '06 #3
Hi,

LaughOutLoud wrote:
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=1 111"
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 <divtag 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=1 111"
onclick="getid( this );return false;">click me</a>
<div id="display">&n bsp;</div>

with

function getid( nLink )
{
if ( nLink
&& nLink.href != null
&& nLink.href.inde xOf( "=" ) -1 )
{
var strId = nLink.href.spli t( "=" )[1];
var nDisplay = document.getEle mentById( "display" );
if ( display )
{
display.firstCh ild.nodeValue = strId;
}
}
}

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 27 '06 #4
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?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '06 #5
Laurent Bugnion said the following on 9/27/2006 10:15 AM:
Hi,

LaughOutLoud wrote:
>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=1 111"
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 <divtag 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=1 111"
onclick="getid( this );return false;">click me</a>
<div id="display">&n bsp;</div>

with

function getid( nLink )
{
if ( nLink
&& nLink.href != null
&& nLink.href.inde xOf( "=" ) -1 )
{
var strId = nLink.href.spli t( "=" )[1];
var nDisplay = document.getEle mentById( "display" );
if ( display )
{
display.firstCh ild.nodeValue = strId;
}
}
}
Possibly simpler:

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

<a href="http://www.domain.com/index.html?id=1 111"
onClick="getid( this.href.subst ring(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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '06 #6
Hi,

Randy Webb wrote:
Possibly simpler:

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

<a href="http://www.domain.com/index.html?id=1 111"
onClick="getid( this.href.subst ring(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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 27 '06 #7
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.


Laurent Bugnion wrote:
Hi,

Randy Webb wrote:
Possibly simpler:

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

<a href="http://www.domain.com/index.html?id=1 111"
onClick="getid( this.href.subst ring(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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.