How to call script in iframe from parent page?

I am trying to call a script in an IFrame from the parent page from the code behind. C # I use to call a function:

protected void Page_Load(object sender, EventArgs e) { ... string scr = "document.getElementById('mapframe').contentWindow.addPoint(0, 0);" ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), scr , true); } 

iFrame HTML:

  <iframe name="mapframe" id="mapframe" src="Map.html" style="width:100%;height:360px;"></iframe> 

And Javascript in IFrame:

  function addPoint(lat, lon) { var myLatlng = new google.maps.LatLng(lat, lon); var marker = new google.maps.Marker({ position: myLatlng, map: map, title: "Hit" }); } 

However, this causes this error: "Unable to get the" addPoint "property from undefined or null reference." What causes this error? I checked that the contentWindow was not null.

+4
source share
2 answers

The problem is that your iframe does not load when you try to call this function

Try to execute

 document.getElementById('mapframe').onload = function() { // I prefer frames.mapFrame.addPoint(); document.getElementById('mapframe').contentWindow.addPoint(0,0); } 

Or even

 <iframe name="mapframe" id="mapframe" src="Map.html" style="width:100%;height:360px;" onload="this.contentWindow.addPoint(0,0)"></iframe> 
+5
source

after loading the iframe after calling the add point function. or you can put a call to this function in the onload

0
source

All Articles