Click the button (or hyperlink) to jump to another page and execute the JS function of the target page

Source: Internet
Author: User

Page jump executes the JS code $ (function () {}); URL parameter passing

The first part of the title does not have to be mentioned, the button can also be replaced with hyperlinks.

Assume that you need a button on the a page, click to jump to the B page, pass some parameters and the B page of a JS function can execute ~ ~

The key is how to jump to the target page and "then" execute the "JS function of the target page". The reason is that a certain JS function of the target page is the one that needs to click the page button or hyperlink to trigger, but now need to jump to B page immediately after the execution!

I don't want to jump from page A to page B, assuming you want to do it on page B, you need to write the code logic in the $ (function () {}); This is the idea of a breakthrough.

We define a button on page A to trigger the action shown in title:

HTML code
    1. <button type="button" class="btn btn-info" onclick="OpenReport ()"> Open Report </button>

It corresponds to the JS function:

JS Code
    1. function OpenReport () {
    2. window.open ("B-page url?flag=true&a=" +a,' NewWindow ',' height=1100,width=1400 ');
    3. }

At the same time, 2 parameters were passed to page B of target page: flag and A.

The $ (fucntion () {}) of the JS file on page b; Add this section of code (key), such as:

JS Code
  1. var url = window.location.href; //Get the URL of the current browser
  2. index = url.indexof ("flag"); To determine if the current URL has flag, if any, the description is to jump from a page, execute the following program
  3. if (index!=-1) {//jump from page A to
  4. //Parameter Preparation
  5. var start = Url.indexof ("A");
  6. var a = url.substring (start + "a". length+1);
  7. var date = new Date ();
  8. var year = Date.getfullyear (); //When the year before
  9. var month = Date.getmonth () + 1; //Current month (month is starting from 0, so must be added 1)
  10. month = month<10?  ("0" +month): Month;
  11. var querydate = year + month;
  12. //+ "&suffix=" + encodeURI (encodeURI (suffix));
  13. var url= "Ajax URL?"  A= "+ A +" &querydate= "+ querydate;
  14. //With parameter execution logic
  15. $.ajax ({
  16. Url:url, DataType:"html", headers:{"x-fragment":"_"},
  17. Cache:false,
  18. Success: function (HTML) {
  19. //Logical processing after a successful response
  20. }
  21. });
  22. }

This code is very typical, for the a page passed over the parameters of the receive in the code's comments are explained in great detail.

By the way, also review the JS get system year and month usage.

The magical magic of the flag variable here is a clever ~ ~ ~

From: http://raising.iteye.com/blog/2283257 Iteye in the best effort

Click the button (or hyperlink) to jump to another page and execute the JS function of the target page

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.