views:

3043

answers:

8

I have some code specific to sorting tables. Since the code is common in most pages I want to make a JS file which will have the code and all the pages using it can reference it from there.

Problem is: How do I add jquery, and table sorter plugin into that js file?

I tried something like this

document.writeln('<script src="/javascripts/jquery.js" type="text/javascript"></script>');
document.writeln('<script type="text/javascript" src="/javascripts/jquery.tablesorter.js"></script>');

but this seems to not work.

whats the best way to do this?

A: 

Why are you using Javascript to write the script tags? Simply add the script tags to your head section. So your document will look something like this:

<html>
  <head>
    <!-- Whatever you want here -->
    <script src="/javascripts/jquery.js" type="text/javascript"></script>
    <script src="/javascripts/jquery.tablesorter.js" type="text/javascript"></script>
  </head>
  <body>
    The contents of the page.
  </body>
</html>
Sinan Taifour
I am talking about JS files. how to add jQuery reference to a JS file
Jash, you can't do it. Script files can't be referenced in other script files.
Canavar
Yes they can, Canavar. AS far as the browser is concerned, it doesn't matter whether you're adding script tags in the HTML or in a script itself, because it interprets the result the same.
Salty
A: 

If document.write('<\script ...') isn't working, try document.createElement('script')...

Other than that, you should be worried about the type of website you're making - do you really think its a good idea to include .js files from .js files?

ryansstack
A: 

just copy the code from the two files into your file at the top.

or use something like this http://code.google.com/p/minify/ to combine your files dynamically.

Josh

Josh
+2  A: 
var script = document.createElement('script');
script.src = 'http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
Daniel Moura
+1  A: 

The problem is you're using within the script, which is ending the script tag. Try this:

document.writeln('<script src="/javascripts/jquery.js" type="text/javascript"></sc'+'ript>');
document.writeln('<script type="text/javascript" src="/javascripts/jquery.tablesorter.js"></sc'+'ript>');
Salty
A: 

No, you can't reference a script file in another script file, you should link both of them in your page.

Canavar
+1  A: 

Theres a plugin for jquery where you can just include the files you need into some other js file, here is the link for it http://tobiasz123.wordpress.com/2007/08/01/include-script-inclusion-jquery-plugin/.

Also this document.write line will write the script tags in the html not in your js file.

So I hope this could help you out, a little with your problem

chermosillo
A: 

Guys, Here is the solution, that I adopted as a combination of some proposed solutions in some other forums.

This way you can reference both css files and other js files in one js file, thus making change next time only in a single place. Please let me know if you have any concerns on it.

I have done following: 1) I have created a js with name jQueryIncluder.js 2) declared and executed following code in this file ====================================================================

function getVirtualDirectory() { var vDir = document.location.pathname.split('/'); return '/' + vDir[1] + '/'; }

function include_jQueryFilesToPage() {

var siteAddress = location.protocol + '//' + document.location.hostname + getVirtualDirectory();

var jqCSSFilePath = siteAddress + 'includes/jQueryCSS/ehrgreen-theme/jquery-ui-1.8.2.custom.css'; var jqCoreFilePath = siteAddress + 'includes/jquery-1.4.1.min.js'; var jqUIFilePath = siteAddress + 'includes/jquery-ui-1.8.2.custom.min.js';

var head = document.getElementsByTagName('head')[0];

// jQuery CSS jnclude var jqCSS = 'cssIDJQ'; // you could encode the css path itself to generate id. if (!document.getElementById(jqCSS)) { var link = document.createElement('link'); link.id = jqCSS; link.rel = 'stylesheet'; link.type = 'text/css'; link.href = jqCSSFilePath; link.media = 'all'; head.appendChild(link); }

// Core jQuery include var jqc = "coreFileRefIDJQ";
if (!document.getElementById(jqc)) document.write('');

// jQueryUI include var jqUI = "uiFileRefIDJQ";
if (!document.getElementById(jqUI)) document.write('');

}

include_jQueryFilesToPage(); ====================================================================

3) I referenced the above jQueryIncluder.js file in another js or xsl file of my .Net project as following:

I hope my effort is appreciated.

Thanks

TopGun743