admin管理员组文章数量:1333665
Hello guys I have some problem with my website. Here's the situation:
Page 1
<script type="text/javascript" src="jscript.js">
<input type="button" value="change" onClick="changeAttr()">
Page 2
<script type="text/javascript" src="jscript.js">
<input type="text" value="Hello" id="dynamictext">
jscript.js
function changeAttr(){
document.getElemenyById('dynamictext').value="World";
}
Now these 2 pages are open on different tabs. What I want to happen is whenever the button on page 1 is clicked, the value of input text on page 2 will change to "World". How can I make this possible with Javascript or Jquery?
Hello guys I have some problem with my website. Here's the situation:
Page 1
<script type="text/javascript" src="jscript.js">
<input type="button" value="change" onClick="changeAttr()">
Page 2
<script type="text/javascript" src="jscript.js">
<input type="text" value="Hello" id="dynamictext">
jscript.js
function changeAttr(){
document.getElemenyById('dynamictext').value="World";
}
Now these 2 pages are open on different tabs. What I want to happen is whenever the button on page 1 is clicked, the value of input text on page 2 will change to "World". How can I make this possible with Javascript or Jquery?
Share Improve this question edited Oct 7, 2013 at 18:09 clyde asked Oct 7, 2013 at 18:02 clydeclyde 612 silver badges9 bronze badges 04 Answers
Reset to default 6The 1st tab has the task to change a value in localStorage.
localStorage.setItem('superValue', 'world');
Meanwhile the 2nd tab would be "listen" to changes on that localStorage value:
var dinamictext = document.querySelector('dinamictext');
setInterval(function () {
if (dinamictext.value !== localStorage['superValue']) {
dinamictext.value = localStorage['superValue'];
}
}, 100);
This of course works only with pages on the same domain.
You cannot directly access DOM elements from one tab to another. That would be a serious security issue.
- You can municate between your two pages (assuming they are both under your control) using cookies. See this other SO question on the subject.
- You can also use LocalStorage API, assuming you are targeting only modern browsers. See this SO answer.
Both methods will allow you to share data. Then, you will have to define your own protocol, in order to manipulate DOM according to the received mand.
You can use HTML5 Storage. Here is a simple example
The other answers are perfect if both pages belong to the same site. If they're not on the same site however, you'd need a server solution. One page would send a request to the server, and the other page would also have to call the server looking for instructions, and execute those instructions when received. In that scenario, if they're on separate sites, AJAX probably wouldn't work, and you'd have to resort to something like JSONP.
本文标签: javascriptChange element attribute of another pageStack Overflow
版权声明:本文标题:javascript - Change element attribute of another page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742238949a2438557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论