简体   繁体   English

是否可以在一个文档中使用JavaScript来更改另一个文档中的HTML?

[英]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: 根据您的用户体验,有一些选择:

  1. While on the first page, use some javascript to set a cookie and then when the second page loads, read that cookie and have the javascript in the second page adapt based on the cookie value. 在第一页上,使用一些javascript来设置cookie,然后在第二页加载时,读取该cookie并让第二页中的javascript根据cookie值进行调整。 Cookies can be shared between pages on the same domain. Cookie可以在同一域中的页面之间共享。
  2. While on the first page, use some javascript to create a query parameter (those things after the ? in a URL that look like this ?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上的查询参数并决定如何修改自己。这是将临时参数从一个页面传递到下一个页面的最简单方法页。
  3. Load the second page into an iframe (embedded into the first page) and when it's loads, your javascript can modify it (if it is served from the same domain as your main page). 将第二页加载到iframe(嵌入到第一页)中,当加载时,您的javascript可以修改它(如果它是从与主页相同的域提供的)。
  4. Load the second page into your first page, actually inserting it into the original page either appending it or replacing some of your existing content. 将第二页加载到您的第一页,实际将其插入原始页面,或者将其添加或替换一些现有内容。 Then, the first page's javascript can modify the HTML from the second page once it has been inserted. 然后,第一页的javascript可以在插入后从第二页修改HTML。
  5. Open a new window with the new page in it. 打开一个新页面,其中包含新页面。 If it's on the same domain as you, then your javascript can reach into that new page and modify it. 如果它与您在同一个域中,那么您的javascript可以进入该新页面并进行修改。

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.readywindow.onload

newWin.onload = function(){
  var ident = newWin.document.getElementById('identification');
  ident.href = 'http://stackoverflow.com';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用HTML / Javascript浏览其他网站 - Is it possible to use HTML/Javascript to browse another website 可能使用javascript将html td更改为链接? - possible to use javascript to change a html td into a link? 是否可以在一个javascript文件中声明一个变量并在另一个javascript文件中使用它? - Is it possible to declare a variable in one javascript file and use it in another one? 是否可以抓取一页以使用javascript和html将其呈现到另一页? - is it possible to crawl one page to render it to another page in javascript and html? 是否可以使用Javascript从另一个网页更改一个网页? - Is it possible to make a change to one webpage from another webpage with Javascript? 是否可以使用JavaScript从一个打开的页面跳转到另一个页面? - Is it possible to use JavaScript to jump from one open page to another? 是否可以使用 JavaScript 更改 document.readyState? - Is it possible to change document.readyState with JavaScript? 是否可以在JavaScript中更改document.activeElement? - Is it possible to change document.activeElement in JavaScript? 是否可以在Javascript中使用HTML Helpers? - Is it possible to use HTML Helpers in Javascript? 使用 Javascript 中的弹出窗口将变量从一个 html 文档传输到另一个 - Transfering a variable from one html document to another with a popup-window in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM