admin 管理员组文章数量: 1086019
I need to change content of page1 if page2 contain specified element. This code works great if I get id from the same page
if (document.getElementById("page_element")) {
var str = document.getElementById("destination").innerHTML;
var n = str.replace("Login", "Logout");
document.getElementById("destination").innerHTML = n;
}
But I need to check if element is present on page2 in order to change content of page1.
I must change this
if (document.getElementById("page1_element"))
with correct path which is page2 (logged.html in my case). How can I do that ?
I need to change content of page1 if page2 contain specified element. This code works great if I get id from the same page
if (document.getElementById("page_element")) {
var str = document.getElementById("destination").innerHTML;
var n = str.replace("Login", "Logout");
document.getElementById("destination").innerHTML = n;
}
But I need to check if element is present on page2 in order to change content of page1.
I must change this
if (document.getElementById("page1_element"))
with correct path which is page2 (logged.html in my case). How can I do that ?
Share Improve this question edited Mar 14, 2013 at 12:17 marius asked Mar 14, 2013 at 12:06 mariusmarius 3291 gold badge2 silver badges16 bronze badges 6- 1 This is Javascript not Java :) Best to know what language you are writing in – cowls Commented Mar 14, 2013 at 12:08
- 2 Two pages in different tabs/windows or a 2nd page within a frame? Pages on the same domain? – Alex K. Commented Mar 14, 2013 at 12:09
- You can use ajax to check for the element, if you have access to page2, using some framework would be helpful, like jQuery – kidwon Commented Mar 14, 2013 at 12:12
- I have one jquerry function that replace div id on page1 with content of need it div from page2. I use this jQuery(function($){ $('#result').load('Page2.html #intro'); but its not in source code of page1 although I can see correct result in browser. – marius Commented Mar 14, 2013 at 12:17
- I have 2 separate pages. Page1 (mysite./page1.html) must look for div id in page2 (mysite./page2.html) – marius Commented Mar 14, 2013 at 12:26
3 Answers
Reset to default 2Think this will work...
var iframe = document.getElementById("iframeId"),
doc = getFrameDocument(iframe);
doc.getElementById("page2_element_id"); // [DOM Element]
function getFrameDocument(iframe){
return iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
}
return cond ? a : b
means:
if ( cond ) {
return a;
}
else {
return b;
}
Think of it this way. JavaScript runs in the browser, say, Chrome. Chrome can see the page that is currently loaded, but has no knowledge of the html files that are still on the server, until they are brought into the browser. What you could do is have an iFrame containing logged.html that isn't visible (maybe 1x1 pixels, or a fixed position with a position off the page (left: 4000px). Then you can refer to the iFrame and get the element from there, this one explains how to reference an element in an iFrame: getElementById from iframe
I think this might help you... Don't be too hard on me. This is my first post :p
var element_change_content_page1 = "element_id_page1";
var page2_datatype = "html";
var filename_page2 = "logged.html";
var target_element_id_page2 = "element_id_page2";
var target_element_type_page2 = "div"
$.get( filename_page2 , function(response, status){
//You can also use jQuery " $.post() " Method instead of " $.get() "
//Source: http://www.w3schools./jquery/ajax_get.asp
var data = $('<div>' + response + '</div>');
var target_element = data.find( target_element_type_page2 + '#'+ target_element_id_page2 )[0]; //There "might" be more div elements with the same id: specify index
if(typeof target_element !== 'undefined'){
//Page2 contains specified element
var container = document.getElementById( element_change_content_page1 );
container.innerHTML = "Content is changed!!!";
} else {
//Page2 DOES NOT contain specified element
}
}, page2_datatype);
本文标签: javascriptGet element by Id from another pageStack Overflow
版权声明:本文标题:javascript - Get element by Id from another page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744098561a2533355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论