Basic JavaScript Q & A 4_basic knowledge

Source: Internet
Author: User
Link in the JavaScript drop-down menu Ii. Enhanced navigation

1. Link (Links in Select Menu) in the drop-down Menu)

Q: How can I link to different pages in the drop-down menu?

A: Create a drop-down menu as shown in the following figure: Select a pageJavaScript FAQNumbersStringsNavigationColorsJavaScripter.net

You can use the following code:

  1. <Form>
  2. <Select
  3. OnChange= "If (this. selectedIndex! = 0)
  4. Self. location=This. Options [this. selectedIndex]. value">
  5. <Option Value=""Selected>Select a page
  6. <Option Value="Startpag.htm">JavaScript FAQ
  7. <Option Value="Numbers.htm">Numbers
  8. <Option Value="Strings.htm">Strings
  9. <Option Value="Navigati.htm">Navigation
  10. <Option Value="Colors.htm">Colors
  11. <Option Value=Http://www.javascripter.net">JavaScripter.net
  12. Select>
  13. Form>

You only need to change the menu item and its corresponding URL to your need. You can use absolute addresses (like http://www.javascripter.net), or relative addresses (like mypage.htm ).

2. Button Link)

Q: How can I change a button to a hyperlink pointing to another page?

A: To create a button is like:

You can use this code:

  1. <Form>
  2. <Input Type=Button
  3. Value="Insert button text here"
  4. OnClick="Self.location+'your_url_here.htm '">
  5. Form>

You only need to change it to the button text and target address you need. Try this:

You can use an absolute address (like a http://www.javascripter.net consumer can also use a relative address (like mypage.htm ).

3. Back Button)

Q: Can I make the button the same as the "back" button in the browser?

A: Create your ownBackYou can use this code:

  1. <Form>
  2. <Input Type=Button Value="Back"
  3. OnCLick="History. back ()">
  4. Form>

Try it now:

4. Forward Button)

Q: Can I make the button the same as the "Forward" button in the browser?

A: To create your own "Forward" button, use this code:

  1. <Form>
  2. <Input Type=Button Value="Forward"
  3. OnCLick="History. forward ()">
  4. Form>

IfForwardThe button is currently unavailable.Forward"Button cannot work. In this case, the current page is the last page in your browsing history. In other words, if you use theBackThe page (orBack), Then the forward button can work. Try it now!

5. Query string (Query Stirngs)

Q: Can I access the query string in the current URL?

A: The query string (or search string) is an optional part of the URL. It follows the file name and is guided by a question mark (?). For example, the following URL contains a query string after the HTML file name? Myquery:

Http://www.myfirm.com/file.html? Myquery

Your script can use the location. search property of JavaScript to access the query characters in the current URL. Click the following button to try it out! (To view the URL in the address, you may want to display the page in the top-layer browser window .)

The code for creating these buttons is:

  1. <Form>
  2. <Input Type=Button Value="Add query? Test"
  3. OnClick="SelfSelf. location=
  4. Self. location. protocol + '//'
  5. + Self. location. host
  6. + Self. location. pathname + '? Test '">
  7. <Input Type=Button Value="Show query"
  8. OnClick="Alert ('query string: '+ self. location. search )">
  9. <Input Type=Button Value="Remove query"
  10. OnClick="SelfSelf. location=
  11. Self. location. protocol + '//'
  12. + Self. location. host
  13. + Self. location. pathname">
  14. Form>

Note:Query strings may not work as expected. For example, if you save the page to a local disk, the page above will not work in Internet Explorer 4.x (but it will still work in Netscape Navigator ).

6. Pass parameters to the page (Passing parameters to a page)

Q: Can I pass parameters to another page?

A: Yes. There are several different ways to achieve this:

Related Article

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.