JavaScript How can I run a script in another (newly-opened) tab?

I am trying to run a script in a new tab. The code I use is this:

$ = jQuery;
function openAndPush(url, id) {
    var win = window.open('https://example.com' + url + '?view=map');
    var element = $('<script type="text/javascript">console.log("Starting magic...");var region_id='+id+';$=jQuery;var p=$(\'div["se:map:paths"]\').attr(\'se:map:paths\');if(p){console.log("Found! pushing..."); $.get(\'https://localhost:9443/addPolygon\', {id: region_id, polygon: p}, function(){console.log("Done!")})}else{console.log("Not found!");}</script>').get(0);
    setTimeout(function(){ win.document.body.appendChild(element); 
    console.log('New script appended!') }, 10000);
}

Considering the following:

  • I was inspired in this answer, but used jQuery instead.
  • I run this code in an inspector/console, from another page in https://example.com (yes, the actual domain is not example.com - but the target url is always in the same domain with respect to the original tab) to avoid CORS errors.
  • When I run the function (say, openAndPush('/target', 1)) and then inspect the code in another inspector, one for the new window, the console message Starting magic... is not shown (I wait the 10 seconds and perhaps more). However the new DOM element (this script I am creating) is shown in the Elements tab (and, in the first console/inspector, I can see the New script appended! message).

(In both cases jQuery is present, but not occupying the $ identifier, which seems to be undefined - so I manually occupy it)

What I conclude is that my script is not being executed in the new window.

What am I missing? How can I ensure the code is being executed?

Answer:1

Instead of embedding script element in the document, why not wrap your the code that you want to run in another tab into a function and bind that to the new tab's window then call that function.Here's the code that I ran in my browser's console and it worked for me i.e another tab was opened and alert was displayed.

function openAndPush(url, id) {
    var win = window.open('https://www.google.com');
    win.test = function () {
        win.alert("Starting magic...");

    }
    win.test();
    setTimeout(function () {
        win.document.body.appendChild(element);
        console.log('New script appended!')
    }, 10000);
}
Answer:2

I have a function that catches whether the input has changed or not then if the value is exceeding the max it sets the value to the max. So you can not enter a value over the max. But if you try to do ...

I have a function that catches whether the input has changed or not then if the value is exceeding the max it sets the value to the max. So you can not enter a value over the max. But if you try to do ...

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

  1. individual value at risk
  2. individual value of privacy
  3. individual value in excel
  4. individual value of friendship
  5. individual value of human life
  6. individual value of
  7. individual value in english

How can I display this code &#8203; for the user to copy without adding spaces I tried to add code tags <code>&#8203;</code> but didn't work how can I do it ?

How can I display this code &#8203; for the user to copy without adding spaces I tried to add code tags <code>&#8203;</code> but didn't work how can I do it ?

  1. html display blank image
  2. html display blank space
  3. display blank html

I'mm trying to design a specific type of navbar in javascript/jquery. I cannot get mouseenter() and mouseleave() to work correctly when the mouse passes between the li objects. Here is my code. Any ...

I'mm trying to design a specific type of navbar in javascript/jquery. I cannot get mouseenter() and mouseleave() to work correctly when the mouse passes between the li objects. Here is my code. Any ...

  1. mouseenter mouseleave not working
  2. jquery mouseenter mouseleave not working
  3. mouseenter mouseleave