Several Methods for dynamically specifying hyperlink parameters (passing a javascript variable into href)

Source: Internet
Author: User

Scenario: in some environments, we need to determine the parameters of a (some) hyperlink based on the values of JavaScript variables on the page, such as"Http://www.bla.com/test.aspx? Var1 ="To determine how much var1 is equal to, it must be determined based on JavaScript variables.

 

Method 1: simple, client script

<AHref= "#"Onclick= "Redirecturl ();"Class= "Parent">Mark</A> 

 

Function Redirecturl (){
VaR Jsvar1;
VaR Jsvar2;
Window. Location="Http://www.bla.com/test.aspx? Var1 ="+Jsvar1;//Jsvar1 is a javascript variable.
}

 

Method 2: Assign the easiest way wocould be to add the runat = "server" property to the anchor and set the href in the codebehind when loading the server

 

<AID= "Youranchor"Href= "#"Class= "Parent"Runat= "Server">Mark</A>

 

Private VoidPage_load (Object Sender, system. eventargs E)
{
Youranchor. href= Redirecturl ();
}

private string redirecturl ()
{< br> string jsvar1 = " " ;
string jsvar2 = " " ;

 Return "Http://www.bla.com/test.aspx? Var1 =" +Jsvar1;
}

 

Method 3: the customer complained that there are many such hyperlinks on the page, so it is not suitable to write an onclick script for each hyperlink. After thinking about it, there are also ways to facilitate the management of a large number of dynamic hyperlinks:

 

<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HtmlXmlns= "Http://www.w3.org/1999/xhtml">
<HeadRunat= "Server">
<Title>Untitled page</Title>
<ScriptLanguage= "JavaScript"Type= "Text/JavaScript">
//<! CDATA [
VaRJsvar1="222"
Function redir (href)
{< br> document. getelementbyid ( " hidden1 " ). value = (href + Jsvar1 );
Document. getelementbyid ("Button1" ). Click ();
}

// ]>
</Script>
</Head>
<Body>
<FormID= "Form1"Runat= "Server">
<Div>
<AHref= "Http://www.bla.com/test.aspx? Var1 ="Onclick= "Redir (this. href); Return false ;">Mark</A>  
</Div>
<ASP: buttonStyle= "Display: none"ID= "Button1"Runat= "Server"Text= "Button"Onclick= "Button#click" />
<InputID= "Hidden1"Type= "Hidden"Runat= "Server" />
</Form>
</Body>
</Html>

 

 

Protected VoidButton#click (Object sender, eventargs e)
{< br> scriptmanager. registerstartupscript (page, This . getType (), " key " , " window. location = ' " + This . hidden1.value + " ' " , true );
}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.