سؤال

i have this code but doesn't work

index.html

<html>
<body>
 <section>

 </section>
</body>
</html>

welcome.html

<div>Welcome, please login</div>
<div id='login'> </div>

file.js

$('section').load('welcome.html');                          
$('#login').load('login.html');

but if i put an alert like this, and then close the alert it works

$('section').load('welcome.html');

alert("!!");                        
$('#login').load('login.html');

what can i do? i don't want that alert, thank you for the your time

هل كانت مفيدة؟

المحلول 2

The issue you're seeing is due to the fact that jQuery.load() works asynchronously.

Therefore, when you place the alert() in between the two calls, you give a chance to the first call to complete, and then the second call works. Without that alert(), the first load() hasn't completed yet when the second load() is called.

Use a callback function to fix:

$('section').load('welcome.html', function() {
    $('#login').load('login.html');
});

نصائح أخرى

As $.load() function is asynchronous you need to wait for the callback to do the next load();

$('section').load('welcome.html', funcion() {
    $('#login').load('login.html');
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top