javascript - loading external file using jquery and then making reference to it -


i have js file loads content (external html file) dom , alerts content loaded

boo.html

<div id="tyler">    tyler </div> 

play.js

$('#result').load('../html/boo.html'); var mike = $('#tyler').text(); alert(mike); 

file.html

<html>     <head>         <meta http-equiv="content-type" content="text/html; charset=utf-8" />         <title>a</title>      </head>     <body>         <div id="result"></div>         <script type="text/javascript" src="play.js">     </body> </html> 

for strange reason when alert(mike) blank. cant append div id , later reference new div injected?

enter image description here

.load() asynchronous. goes off , thing , code keeps executing in-line. if check content immediately, won't there yet. takes while server respond, send html , insert dom.

you need use callback this. callback event fired when asynchronous event complete.

$('#result').load('../html/boo.html',function() { alert("moo, callback") }); 

http://api.jquery.com/load/


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -