简体   繁体   中英

Way to simulate running javascript in web browser through using javascript itself

So what I'm trying to do is load window.open a page except the page that opens lacks scrollbars so what I tried to do was:

var script = win.document.createElement('script');
script.text = "$('body').css('overflow', 'auto');";
win.document.body.appendChild(script);

and obviously I get the permission denied error because of CORS. Anyhow, so I try running:

$("body").css("overflow", "auto"); 

via the web browser console (firefox) and it works; the scrollbar appearing as expected.

So all in all, I was wondering if there was a way to simulate running $("body").css("overflow", "auto"); on the web browser through javascript to get the same effect, and thus bypassing the same-origin error/protocol

(I hope it wasn't too verbose/confusing :D)

If your code is sitting in another window or frame and your code comes from a different domain than the window you are trying to modify, then you cannot change that other window in any way. The browser will simply not permit access. You can't directly modify and you can't inject Javascript into it.

That is a very important security protection and there is no way around it from plain Javascript in a web page.

If the other page wishes to cooperate with you, then you can use window.postMessage() to communicate with it, but that takes two cooperating pieces of Javascript, one in your window and one in the target window in order to effect a change. If the other window is not your code and does not have a cross origin interface for making changes, then you can't use window.postMessage() with it.

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