是否可以在一个文档中使用JavaScript来更改另一个文档中的HTML? [英] Is it possible to use JavaScript in one document to change HTML in another?

查看:77
本文介绍了是否可以在一个文档中使用JavaScript来更改另一个文档中的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

function change()
{
document.getElementById('identification')。href =http://www.stackoverflow.com;
}

关联的HTML(重要位)

The associated HTML (the important bit)


< a href =#id =identification> Stack Overflow< / a>

< input type = button value = 更改onclick =change()/>

这会将我的标签中的href更改为 http://www.stackoverflow.com ,但是我想从不同的HTML文件中执行此操作? JavaScript将位于另一个文件的标记中,但会编辑第一个文件的内容。这可能吗?如果是这样,怎么做?

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? The JavaScript would be in the tag of the other file, but would edit the content of the first. Is this possible? If so, how?

推荐答案

Javascript仅存在于特定页面的生命周期中,因此您无法将代码放在一个文件中修改另一个尚未卸载的文件。

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.

根据您的用户体验,有以下几种选择:

Depending upon what you want the user experience to be, there are some options:


  1. 在第一页上,使用一些javascript来设置cookie,然后在第二页加载时,读取该cookie并让第二页中的javascript根据cookie值进行调整。 Cookie可以在同一个域中的页面之间共享。

  2. 在第一页上,使用一些javascript来创建一个查询参数(在URL之后的那些东西看起来像这样?show = true 。当您加载第二页时,通过附加?show = true (或其他)请求该页面你编写了到URL的末尾。当第二个页面加载时,它可以检查它的URL上的查询参数,并决定如何修改自己。这是将临时参数从一个页面传递到下一页面的最简单方法。

  3. 将第二页加载到iframe(嵌入到第一页)中,当它加载时,您的javascript可以修改它(如果它是从与主页面相同的域提供的)。

  4. 将第二页加载到第一页,实际将其插入到原始页面中,或者将其添加到原始页面中,或者替换一些现有内容。然后,第一页的javascript可以修改HTML中的HTML第二页插入后。

  5. 打开一个新页面,其中包含新页面。如果它与您在同一个域中,则您的javascript可以进入该新页面并进行修改。

  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. Cookies can be shared between pages on the same domain.
  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.
  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).
  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.
  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 (e.g. 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来更改另一个文档中的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆