Nesting JavaScript includes

They have: 10 posts

Joined: Jun 2007

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

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's picture

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...

He has: 629 posts

Joined: May 2007

netsperience suggested:

document.write ('<scr' + 'ipt type="text/javascript" src="js2.js"></scr' + 'ipt>');

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's picture

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.

He has: 629 posts

Joined: May 2007

What situation would the page be "served" as xml... ?

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's picture

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?

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's picture

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:

Gecko implementation of createElement doesn't conform to the DOM spec for XUL and XHTML documents: localName and namespaceURI are not set to null on the created element.

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.