Jump to content
xisto Community
Sign in to follow this  
bluefish1405241537

Problems With Dynamically Loading Javascript As well as dynamically inserting HTML

Recommended Posts

Hello,

I have started to try to create a JavaScript application (or rather, AJAX, but the JavaScript is the part I'm having trouble with). I have come across a roadblock, though. I try to load an external JavaScript file by editing the innerHTML of a div tag to contain <script src="URL" type="text/javascript"> (with a real URL). The problem is that it is not loading. I have used FireBug to check the dynamic HTML status, I get the following (with code removed):

<html><head/><body><div id="status"/><div id="main"/><script type="text/javascript"></script><div id="scripts"><!--START DYNAMIC JAVASCRIPT--><script src="source/main_login.js" type="text/javascript"></script><!--END DYNAMIC JAVASCRIPT--></div><script src="source/main.js" type="text/javascript"></script></body></html>

I have done extensive testing and am sure that main_login is not loading. When I insert the relevant code into the actual file, it works fine. Is there anything I am doing wrong and/or is there a solution?

Note: This could be related to the fact that FireBug does not update the dynamically added code when I change it, but that may just be a bug.


I am also having a problem with inserting HTML into a <div> tag - my tags seem to close themselves, as in the following code. It seems quite possible that the two problems are related.

<div id="main"><div id="main/login"/><form onsubmit="java script:login(this);"/><table/>User Name:<input type="text" name="usr_name"/>Password:<input type="password" name="usr_pass"/><input type="submit" value="Log In"/></div>
(the table, and form should encompass the whole)
Edited by bluefish (see edit history)

Share this post


Link to post
Share on other sites

I don't really like using innerHTML and prefer using DOM methods to manipulate the documents. With innerHTML, you don't need to encode HTML entities, you can simply pass the complete string, e.g. <script type="text/javascript">... When using the DOM you must create the elements, and append the data on them. Although it requires more work, it gives better understanding to your document creation, also not all browsers support innerHTML.As with the automatic closing of each element, I can't explain that. Are you using a script you created yourself, or a script created by someone else?Cheers,MC

Share this post


Link to post
Share on other sites

Thanks MC,I solved the first problem by using DOM. I don't know what the original problem was, but it might have something to do with innerHTML not activating the script object. Anyways, it worked out without any real problems. As for the second problem, it was my fault for appending the lines of HTML separately, when I should have put them in a string and then put the whole string on. InnerHTML automatically closes unclosed tags when it's changed, so it could't tell that I intended to close it later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.