-3

when i am loading a web page using the code window.location . I want to get the text inside the paragraph. when i using the below code i didn't get the answer. How to get the contents of web page when using window.location. The page loading is working successfully. But the content is not getting proper.

$(function(){
    window.location="http://xxxxxxxxx/";
    var x = document.getElementsByTagName("p");
        console.log(x[0]);


    }

The output is showing undefined. I didn't get the correct answer. Is it possible please help me.

Nope
  • 22,147
  • 7
  • 47
  • 72
Fazil fazi
  • 439
  • 1
  • 4
  • 15
  • Your title says `getelementbyid` but your code uses `getElementsByTagName` – j08691 Oct 12 '17 at 14:34
  • You're trying to get the `p` for which page? The original or the newly loaded one? – llama Oct 12 '17 at 14:34
  • Try using `document.querySelector` – Baruch Oct 12 '17 at 14:35
  • 2
    You are navigating away from the page, of course the script after `window.location` won't run. – Krisztián Dudás Oct 12 '17 at 14:39
  • window.location is navigating away and your title is not the same as your content, please fix it. and you will never get a log from this code because of window.location. also even if you are sending it to the same file it will be infinite requests and the browser will discard it... – HSLM Oct 12 '17 at 14:42
  • Your code is working. `window.location` navigates to the new page, however, your `document` is still referencing the page before it moved. And yes, your next line after still executes it's just not looking at the page you think it is looking at. – Nope Oct 12 '17 at 14:43
  • 1
    If your technique worked this would’ve been a serious security issue. – Terry Oct 12 '17 at 14:43
  • @KrisztiánDudás @RolandStarke The code in the old page doesn't stop executing but instead code within the current block should complete, with exceptions, such as `setTimeout` and similar. DOM queries and `console.log()` should continue executing. This SO explains it better ► https://stackoverflow.com/a/2536954/448144 - Either way, in the case of OP `document` refers to the current page and will only find elements on that but will not refer to the new page. – Nope Oct 12 '17 at 14:59

1 Answers1

1

When you use window.location="http://xxxxxxxxx/"; you will navigate to your new page. In this page you don't have your javascript, therefore you don't try to access it.

You can't execute some javascript from a previous page in the next.

sheplu
  • 2,937
  • 3
  • 24
  • 21