Javascript in PHP: HTML shows right code, but isn't called
I reload a div with a php file containing HTML tags and javascript code. I load the div like this.
$("#snews").load("loadnews.php");
The loadnews.php looks like this.
<div id="feed-control">
<span style="color:#676767;font-size:11px;margin:10px;padding:4px;">Loading...</span>
</div>
<script type="text/javascript">
function LoadDynamicFeedControl() {
new GFdynamicFeedControl(feeds, 'feed-control', options);
}
// Load the feeds API and set the onload callback.
google.load('feeds', '1');
google.setOnLoadCallback(LoadDynamicFeedControl);
</script>
When I d开发者_C百科o a view source, the HTML looks OK. It looks exactly like the loadnews.php. It shows the div correctly with the text "Loading..." But the javascript function isn't called. What could be the problem?
You have to call the function. You can do so by typing in something like this after the definition of your javascript function, and certainly inside your loadnews.php, for instance just after the definition of the function. PHP will load after javascript so if you try to call the function from the "main" page the function will not be called as it is not in the code
Here is the snippet:
<script type="text/javascript">
LoadDynamicFeedControl();
</script>
You can assign this function to several user events such as a click on a button or whatever you may imagine.
<form>
<input type="button" value="Click me!" onclick="LoadDynamicFeedControl()" />
</form>
This would execute the function when you click on the button
精彩评论