2

I'm trying execute js function after redirection to the page e.g "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!

J Mascis
  • 103
  • 1
  • 2
  • 8
  • I'm inclined to close this as a duplicate of http://stackoverflow.com/questions/12921345/execute-javascript-on-one-page-from-another?lq=1, but that question is itself a duplicate, and the other duplicate isn't quite the same as what's being asked here. – zzzzBov Feb 04 '16 at 21:38

3 Answers3

4

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).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • "This stops the execution of the Javascript in the current page." actually not quite, some events like the onbeforeunload event will be triggered, and I believe some browsers even allow unload to execute so as to do things like store state in localStorage. – zzzzBov Feb 04 '16 at 21:40
  • 1
    @zzzzBov - I incorporated that into my answer. – jfriend00 Feb 04 '16 at 21:41
  • 1
    @jfriend00 - Maybe it is any way to execute code from local? My Idea: local saving js function as file -> running after window.location. Mayby we can load page with js with paramethers using local scripts. Anyway thanks for quick answer. That is my first post. I'm impressed how quickly I've got answer / explanation. btw sorry for my english :( – J Mascis Feb 04 '16 at 22:48
  • @JMascis - We could help more thoroughly if you told us exactly what you're trying to do to the other page with Javascript. You can't save a file locally with Javascript from normal browser privileges. – jfriend00 Feb 04 '16 at 22:49
  • 1
    @jfriend00 - I would like fill the form-content using data which I've brought using OCR Algorithm – J Mascis Feb 04 '16 at 22:59
  • @JMascis - Is the page you want to load on your domain and something that you control? – jfriend00 Feb 04 '16 at 23:15
  • @jfriend00 - No, but I decide to use `Selenium` here I have what I want. – J Mascis Feb 05 '16 at 09:03
0

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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);
?>
J Mascis
  • 103
  • 1
  • 2
  • 8