Load new page and script afterwards

0

I wanted to know how a web page could be loaded and after being loaded a script is executed. I have tried this way but there is no way.

On the console and in the URL bar:

javascript:window.open("http://www.google.es"); setTimeout(function(){alert("codigo");},2000);
    
asked by marc 04.04.2016 в 20:35
source

3 answers

4

Do you mean that the content of the site loads inside your page?

If so, the window.open () method does not work for you (opens in a new window)

to load a site inside your page and that this one sends a script, can be achieved using iframe

<script>
  document.getElementById("myframe").addEventListener('load', function() {
    alert("cargo sitio");
  });
</script>
<iframe id="myframe" src="http://musicaq.me"></iframe>

If you are trying to inject code to a site other than yours it is impossible because of a security issue link

    
answered by 04.04.2016 / 21:03
source
2

The best way to do this on pages of your project is to use jQuery and the ready method. This is the most basic example:

$(document).ready(function () {
    //aquí el código que deseas ejecutar
});

If you want to open a new window and execute a JavaScript function when you open it and this page is from the same domain of your current project, you can use the following:

var ventana = window.open("http://midominio/nuevaUrl");
ventana.setTimeout(() => alert("codigo"),2000);

Any attempt to open a page outside your domain and add functionality is not allowed because it is cross-domain (unless you add the respective header of CORS and the site allows it).

    
answered by 04.04.2016 в 20:56
0

There is also another way, but it is not automatic ... And it's through "developer tools" from Chrome or similar ones ... For that you must open the page manually and by pressing "Ctrl + Shift + I" or F12 in Google Chrome and go to the "console" tab and there you can paste your Script, whatever the Script does only work for you every time that you do those steps (I do not think it can be done automatically) .......

But I think that's what you need (or needed), I'm also doing something similar ... There is a certain page that does not allow or "import", or "iframe" as they said earlier for security issues, but when you run the script on your already loaded page I can insert the code I want and modify its appearance .... insert Some "iframe" s that is what I needed most and as I do in one of the pages of your domain I can freely use the "iframe".

I like how I'm staying, in case you want to see the page and the script let me know.

    
answered by 15.06.2016 в 04:18