简体   繁体   中英

JQuery - Using $(document).ready() after window.location

I'm trying execute js function after redirection to the page eg "example.com" when DOM is ready to work with him but it doesn't work. $(document).ready() is running before "example.com" is loaded. What is wrong? Here is my code:

window.location = "http://example.com";
$(document).ready(function(){
    //DO SOMETHING WHEN PAGE 'example.com' IS LOADED
});

I would be grateful for your help!

You simply can't do what you're trying to do. When the page is changed to "http://example.com" , this sets in motion a series of events which will eventually result in your entire Javascript context being stopped. You can't run Javascript from this page in the new page.

When you assign:

window.location = "http://example.com";

This begins a process which will stop the execution of the Javascript in the current page. The current page is unloaded and all memory associated with it is freed. Then, a new page is loaded, a new Javascript context is created and the Javascript in that new page is then loaded and run.


The usual way to communicate with a new page is to put the Javascript that wants to run in that new page and then send some parameters to that new page that its Javascript can detect and react accordingly.

For example, you might do this:

window.location = "http://example.com?doSomething=whatever";

And, then when the new page is loaded, the Javascript in THAT page can check the query string to see if any special arguments were passed to it. If they were, then that Javascript can parse them and decide what behavior to carry out based on those query string parameters.


Another possibility is to not use window.location . You can, instead, fetch new HTML via Ajax and then use Javascript in the current page to insert that new content into the current page (or replace existing content with new content). With jQuery, you can use jQuery's .load() to load content from a URL directly into an element in the current page like this:

$("#myDiv").load(someURL);

Since this is Ajax, it has all the normal cross-origin restrictions that any Ajax call might have so you can't freely load pages from other websites (unless they permit cross origin access).

A JavaScript program embedded in a page only runs while that page is open.

If you leave the page, by loading a new one (which is what you are doing), then it stops running.

You can't inject JavaScript on to the next page using just features of the browser. You have to have some mechanism to pass it through the server.

I wanted to have some JavaScript inserting values from www.mypage.com to www.example.com and then submit button is clicked. Thank you for your clarification but I decide to use Selenium with Python and PHP. Or maybe is there any way to do this with JavaScript?

run_search.py

import sys
from selenium import webdriver

def insertContent(myElement):
    driver = webdriver.Firefox()
    driver.get("http://www.google.com")
    elem = driver.find_element_by_id("lst-ib")
    elem.send_keys(myElement)
    BUTTON_XPATH = '//button[@type="submit" @name="btnI"]'
    button = driver.find_element_by_xpath(BUTTON_XPATH)
    button.click()

if __name__ == '__main__':
    insertContent(sys.argv[1])

mypage.php

<?php
    $my_value = "Python";
    $exec_py = shell_exec('python run_search.py '.$my_value);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM