[英]Is it possible to use JavaScript in one document to change HTML in another?
For example: 例如:
function change() { document.getElementById('identification').href = "http://www.stackoverflow.com"; }
The associated HTML (the important bit) 关联的HTML(重要的位)
<a href="#" id="identification">Stack Overflow</a>
<input type=button value="Change" onclick="change()" />
This will change the href in my tag to http://www.stackoverflow.com , but say I wanted to do this from a different HTML file? 这会将我标记中的href更改为http://www.stackoverflow.com ,但是说我想从不同的HTML文件中执行此操作? The JavaScript would be in the tag of the other file, but would edit the content of the first. JavaScript将位于另一个文件的标记中,但会编辑第一个文件的内容。 Is this possible? 这可能吗? If so, how? 如果是这样,怎么样?
Javascript lives only for the life of a particular page so you can't have code in one file modify another, as yet unloaded file. Javascript仅存在于特定页面的生命周期中,因此您无法将一个文件中的代码修改为另一个尚未卸载的文件。
Depending upon what you want the user experience to be, there are some options: 根据您的用户体验,有一些选择:
?show=true
. When you load the second page, request that page by appending the ?show=true
(or whatever you make up) to the end of the URL. When the second page loads, it can examine the query parameters on it's URL and decide how to modify itself. This is the simplest way of passing temporary arguments from one page to the next page. 在第一页上,使用一些javascript来创建一个查询参数(在URL中看起来像这样的东西?show=true
。当你加载第二页时,通过附加?show=true
请求该页面(或者你编写的任何东西)到URL的末尾。当第二个页面加载时,它可以检查它的URL上的查询参数并决定如何修改自己。这是将临时参数从一个页面传递到下一个页面的最简单方法页。 Note: the browser tries to prevent page modifications when the two pages do not have the same origin (eg same domain). 注意:当两个页面不具有相同的源(例如,相同的域)时,浏览器会尝试阻止页面修改。 See a description of the same origin policy . 请参阅相同原始策略的说明 。 So, if your question pertains to pages on different domains, then you will need to find a different way to solve your problem. 因此,如果您的问题与不同域上的页面有关,那么您需要找到一种不同的方法来解决您的问题。 Things like add-ons can sometimes get around the same-origin policy, but regular page javascript cannot for numerous security reasons. 像加载项这样的东西有时可以绕过同源策略,但是由于众多安全原因,常规页面javascript无法实现。
Because I can't find a question that I feel matches this one enough to be closed as an exact duplicate, I'll post an answer: 因为我找不到一个我认为匹配的问题足以完全复制,我会发一个答案:
Is it possible to use JavaScript in one document to change HTML in another? 是否可以在一个文档中使用JavaScript来更改另一个文档中的HTML?
Yes, assuming both windows are within the same security sandbox. 是的,假设两个窗口都在同一个安全沙箱中。
If so, how? 如果是这样,怎么样?
It's quite simple, you need to call the DOM functions from the context of the window
you want to access. 这很简单,你需要从你想要访问的window
的上下文中调用DOM函数。
a simple way to get a new window object is to call window.open
获取新窗口对象的一种简单方法是调用window.open
var newWin = window.open(newpage)
newWin is a window
object, and therefor has a document
object as well as all the other DOM elements that it may have loaded. newWin是一个window
对象,因此它有一个document
对象以及它可能已加载的所有其他DOM元素。 Just like any other window, you'll need to wait for document.ready
or window.onload
if you're trying to interact with the elements being loaded on the page. 就像任何其他窗口一样,如果您尝试与页面上加载的元素进行交互,则需要等待document.ready
或window.onload
。
newWin.onload = function(){
var ident = newWin.document.getElementById('identification');
ident.href = 'http://stackoverflow.com';
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.