假設我通過將url傳遞給window.open()函數來打開一個網頁(www.facebook.com)。因爲我在window.open()函數中使用了「_blank」,所以我的窗口作爲新的瀏覽器打開。在window.open()窗口上運行Javascript
我想在新打開的窗口上運行自己的JavaScript。當我研究時,發現了一些我在源代碼中添加的解決方案,如下所示。但不幸的是,我的JavaScript沒有在新打開的窗口上工作。有人可以幫助我嗎?
<!DOCTYPE html>
<html>
<head>
<script>
\t function myFunction() {
\t \t var url = document.getElementById("myText").value
\t \t var newWindow = window.open(url,'_blank','height=400,width=600,left=10,top=10,scrollbars=yes,menubar=yes,titlebar=yes')
\t \t var newScript = newWindow.document.createElement('script');
\t \t //console.log(newScript);
\t \t newScript.setAttribute('type','text/javascript');
\t \t newScript.setAttribute('src','C:/Users/30216/Desktop/jquery_edited_new1.js');
\t \t newWindow.document.getElementsByTagName("head")[0].appendChild(newScript);
\t \t //console.log(newWindow);
\t \t //newWindow.document.head.appendChild(newScript);
\t \t
}
\t \t
</script>
</head>
<body>
<table align = "center">
<frame>
<tr>
\t <td class="url">Enter the URL:</td>
\t <td>
\t \t <input type="text" id="myText"></input>
\t </td>
</tr>
<tr>
\t <td>
\t \t <button id="browser_open" onclick = "myFunction()">Submit</button>
\t </td>
</tr>
</frame>
</body>
</html>
你不能那樣做。清除跨越原點違例(請參閱https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)。如果你認爲你只是簡單地打開你的網站的Facebook登錄頁面,並捕獲用戶的登錄信息,然後哈哈,忘掉它。 ;) – smajl
如果你想在你選擇的網頁上運行你自己的JavaScript,那麼TamperMonkey/GreaseMonkey插件就是你想要的。 – Phylogenesis