Nesting JavaScript includes
Hi,
I am a little new to javascript and i am wondering if i can next files for example.
<script type="text/javascript" src="js1.js"></script> and in js1.js have this code <script type="text/javascript" src="js2.js"></script>
Will i get the functionality ok js2?
Thanks
webwiz posted this at 19:18 — 6th June 2008.
He has: 629 posts
Joined: May 2007
Nope.
Basically, a .js file interprets JavaScript, not HTML. But you can combine two .js files into one and use the combined file as the SRC to the SCRIPT. A tiny bit more work, but does what you want-- if I understand you correctly.
Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;
decibel.places posted this at 00:40 — 24th June 2008.
He has: 1,494 posts
Joined: Jun 2008
you can do that if you use document.write() to embed the second js file:
<script type="text/javascript" src="js1.js"></script>
and in js1.js have this code:
document.write ('<scr' + 'ipt type="text/javascript" src="js2.js"></scr' + 'ipt>');
notice that I have separated the script tags so they do not interfere with the container script...
webwiz posted this at 01:17 — 24th June 2008.
He has: 629 posts
Joined: May 2007
netsperience suggested:
Well, yes. You could. But bear in mind that document.write does not work in XHTML mode (on XHTML pages served as XML).
Although you are more likely to be using HTML mode for your pages. Just thought I'd mention it.
Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;
decibel.places posted this at 03:50 — 24th June 2008.
He has: 1,494 posts
Joined: Jun 2008
I never had a problem with document.write() but I am mostly adding JavaScript to PHP pages or Lassoscript these days, or HTML.
Do you mean XHTML files using a .xml extension? What situation would the page be "served" as xml, in rss?
I'm not questioning you, but I'm curious, I never encountered a situation like that.
webwiz posted this at 02:50 — 25th June 2008.
He has: 629 posts
Joined: May 2007
A server sends information to the browser about every file it sends. One piece of data is known as the MIME type. For HTML the MIME type is text/html. Any file can be sent as text/html, including php files, .cf files, and so on.
The browser in turn sends information to the server as to what MIME types it can handle. A number of webmasters will send files as one of the possible XHTML MIME types to browsers that can handle it. There are several ways to do this, from server settings to using PHP.
As you are using HTML, the restrictions of XHTML/XML do not apply. Same with XHTML sent as HTML - only the validator takes any notice of the difference - browsers treat the code as regular HTML, not XHTML.
Presumably some of those using XHTML do so to get the benefits of XML-- or why put up with unnecessary restrictions? Hence my caution about using code that will break when they eventually send their elaborately coded pages as the "correct" MIME type.
Hope you can make sense of all this! :\
P.S. One way of sending XHTML is described at Keystone Websites.com.
Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;
decibel.places posted this at 00:51 — 28th June 2008.
He has: 1,494 posts
Joined: Jun 2008
aha
the W3C recommends that the dynamic scripting achieved in HTML with document/write() be managed with DOM in XHTML
http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite
I suppose this means changing innerHTML values on the fly?
webwiz posted this at 04:25 — 30th June 2008.
He has: 629 posts
Joined: May 2007
DOM methods include document.createElement, node.appendChild, and so on.
You can't use innerHTML when serving XHTML as an XML MIME type either. Sorry.
Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;
decibel.places posted this at 21:46 — 30th June 2008.
He has: 1,494 posts
Joined: Jun 2008
ok so all my clientside methods are neutered
what about PHP? Lassoscript?
(I actually work sometimes on a Lassoscript site)
and I found this:
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.