Solved

Importing an external javascript file into XSLT?

Posted on 2007-11-14
3
1,705 Views
Last Modified: 2013-11-18
I need to find out if it is possible to import an external javascript file into an xslt stylesheet.  We recently upgraded our search engine to a Google machine and I am trying to replicate our existing search page so the Google output matches.  Our existing site uses a DHTML/Javascript menu which has 4 rather large files that make it work.  I suppose I could take the content of those files and put it all in a CDATA section but we're talking about several hundred lines - on top of the 3000+ lines of the Google code.  Plus then I'd have to maintain 2 separate files for menu content.  Thus, I'd prefer to reference the external files and maintain one source - if possible.

The files I'm trying to call are:
<script type="text/javascript" src="/js/detectflash.js"></script>
<script type="text/javascript" src="/js/milonic_src.js"></script>      
<script type="text/javascript">
if(ns4)_d.write("<scr"+"ipt type=text/javascript src=/js/mmenuns4.js><\/scr"+"ipt>");      
  else _d.write("<scr"+"ipt type=text/javascript src=/js/mmenudom.js><\/scr"+"ipt>");
</script>
<script type="text/javascript" src="/js/menu_data.js"></script>

Thanks in advance.
0
Comment
Question by:saabStory
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
Comment Utility
Javascript is not special content, or would have a special meaning for XSLT. You can just treat the Javascript <script> tags as you would treat any other html tag inside your XSLT.

However, whether the javascript itself is actually called and parsed by the browser depends both on the browser and on the context (client-side or server-side transformation or an invocation by a processor instruction inside an XML file).

If you are doing a server side transformation, it is easy, just add the elements as literal result elements:

<xsl:template match="/">
   <html>
      <head>
          <script ..... />
          <script>
               if(ns4).write(.....
          </script>
      <body>
      .....
     <xsl:apply-templates select="somenode" />
      </body>
   </html>
 </xsl:template>

there are only a  couple of caveats that have mainly to do with escaping and/or automatic insertion of the wrong CDATA sequence inside the <script> with content in the case of XHTML. I can help you with that if a problem arises, but then I'll need to know the processor.

In the case of a client transformation, I'd recommend downloading (and reading about) prototype.js. An excellent toolchest for doing client side XML. Lookup the functions about running the javascript inside script tags after a transform. You'll have to feed the prototype.js functions the output XML DOM tree.

In the case of a PI, you are out of luck. The caveats and troubles are too high to be overcome easily. Well, it is possible, but you will really end up in browser compat-hell and in a far-from-trivial resolution. But a PI has so many other drawbacks, I assume you haven't even considered it for your situation ;)

Cheers,
-- Abel --
0
 

Author Comment

by:saabStory
Comment Utility
Odd that I didn't get a notification of your answer.  I had actually found the answer on the menu manufacturers site.  There, they indicated that any Gecko based browsers were out of luck due to the document.write - i.e.
if(ns4)_d.write("<scr"+"ipt type=text/javascript src=/js/mmenuns4.js><\/scr"+"ipt>");
  else _d.write("<scr"+"ipt type=text/javascript src=/js/mmenudom.js><\/scr"+"ipt>");

I can verify that these lines to indeed cause problems.  As you indicated, a solution would be nearly impossible due to differeing processors.  Since our browser base is steady at 20% non-IE, we might be better served to drive the results from the search into an iframe and eliminate a lot of hassle.
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Hmm, you still haven't told what your processor and/or context is. From what you write, it looks like client-side. The document.write are a violation of the DOM object model, which is why they do not work on the (more conforming) Gecko browsers, Opera, Safari and Konqeror. In any case, when you see code that still uses "document.write", you should dismiss it as suspicious code and replace it with the DOM (or similar: innerHTML) equivalents.

Using document.write on a conforming XHTML document may yield surprising (jumping!) results when used in IE6 or IE7 which are very hard to track down.

However, funnily enough, the only "allowed" document.write is with script tags. But when written correctly, they work on any browser (see del.iciou.us site, which has a good framework for doing javcascript script-tags injection, which will give you a lot of freedom, esp. because you end up having only one <script> tag per html file).

If you are going to do any serious javascript (meaning: anything larger then 10 lines) you should consider prototype.js.

Thx for the points ;)

Cheers,
-- Abel --
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now