First, you are showing a bit of code with mixed html and javascript, so it is hard to assume what's loaded first in your web page, maybe you should give the complete code.
But assuming you are loading jquery script then index script then running the load code, then there should be either:
- a callback in your load that would trigger the execution of something that you wrote in index.js
- a script tag in somepage.php that would trigger something in your index.js (less recommended, loading mixed html and javascript through ajax can be confusing both for the mind and for the javascript engines!)
- Switch from jquery load() to jquery ajax() who can be configured with "async:false", which is even less recommended than the previous solution, because If read at many places that syncrhonous requests are evil, and also because it is true.
Because right now, probably, your index.js script is trying to do stuff with your html page that is not loaded yet, your page loading being asynchronous, meaning that the execution of whatever is writen in index.js will likely occur before the loading is finished.
So I would do this:
1) Concentrate any execution code you have in index.js into a "afterLoad" function
index.js content:
afterLoad=function(){
alert('Hello World');
}
otherStuff1=function(){
...
}
...
2) Modify the load script accordingly so the callback will be executed once the page is loaded:
$(document).ready(function() { // or click on a button
$('#content').load('somepage.php',function(){
afterLoad();
});
});
And... yes there may be a downside to this approach, with search engines trying to navigate your website and finding only one page. There should be a "fallback" so robots (without a javascript engine) can still browse the pages through classic hyperlinks.