Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById from another page

I am trying to get one div from one webpage URL to another webpage and display in plain text using getElementById without using AJAX or jQuery because I'm going to implement it in FitNesse. Is there a way to pass the URL?

like image 522
Safi Naseen Avatar asked Jan 13 '15 20:01

Safi Naseen


People also ask

How can get ID from another page in HTML?

If you need to get the IDs from the HTML on another HTML page, you have to literally load that page. You can do this programmatically using JavaScript (look up AJAX), but you have to load the HTML page, it doesn't exist somewhere floating around in the ether just waiting for you to grab things from it.

What can I use instead of getElementById?

A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.

How does document getElementById?

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

How do you write document getElementById in HTML?

HTML DOM Document getElementById()The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.


1 Answers

You could load the URL in a hidden iframe.

Then use iframe.contentWindow.document.getElementById($id) as outlined here: How to pick element inside iframe using document.getElementById

Something along the lines of:

<iframe src="urlWithinYourDomain.html" style="display:none"></iframe>

Followed by a function something like:

var divElement = document.getElementById('iframeId').contentWindow.document.getElementById('elementIdOnSourcePage');
document.getElementById('targetParentId').appendChild(divElement);

I'm afraid I can't test this at the moment, so there may be syntax errors, but I think it conveys the idea. It's a bit of a dirty approach, but given your constraints it's the best way I can see to do what you're asking.

like image 183
Mic Avatar answered Oct 16 '22 11:10

Mic