Importing a .js file in another .js file......

Hello,

I am having two javascript files. i want to use the function of 2nd javascript file into my 1st javascript file,

How to do this, ???

In CSS if we want to use another css file then we can do it with @import but how to do the same in case javascript files....?????


Prashant
0
prashant_victory
7/20/2007 7:11:33 AM
asp.net.client-side 24353 articles. 1 followers. Follow

13 Replies
730 Views

Similar Articles

[PageSpeed] 3

Here is one way to do it:

Inside your first javascript file, you could have this function:

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}
Then call it with: include_dom("script2.js") . 
This method also allows you to call the script file only when it is needed. Hope this helps.


Hypercasting™ - Turning the internet into a retail tool. www.muddhypercasting.com
0
joshj
7/20/2007 12:17:42 PM

You would have to declare the first JavaScript file before the second JavaScript file in the HTML page to ensure the function was available to the second file.

i.e.

<script type="text/javascript" src="js1.js" />
<script type="text/javascript" src="js2.js" />

0
Sohnee
7/20/2007 2:06:41 PM

Hello,

Actually i d't want that user will know that i am using second.js file, that means when i include both of these files in my page like below

<script type="text/javascript" src="js1.js" />
<script type="text/javascript" src="js2.js" />

Then user can see this from the browser using ViewSource that i d't want that user know that i am using two files.. you can consider it as a security reason due to which i d't want that user able to know thta i am using two files.. and i d't have any problem if user klnows that i am using a single file.

So that means i want to restrict this thing to the one jabvascript file that using any way i will able to include my foirst file in second script and then able to access methods of that first one.

without including both of these files in the HTML  page.....??????????


Prashant
0
prashant_victory
7/21/2007 8:41:41 AM

Not possible, client scripting can always be viewed through view source. It would be better to put sensitive code in server side scripts.



Hypercasting™ - Turning the internet into a retail tool. www.muddhypercasting.com
0
joshj
7/21/2007 5:45:25 PM

Is your question answered?



Hypercasting™ - Turning the internet into a retail tool. www.muddhypercasting.com
0
joshj
7/23/2007 5:59:55 PM

 if this is not possible then YES it is answered.....


Prashant
0
prashant_victory
7/24/2007 8:23:07 AM

Then mark the post as answered so everyone knows your problem is solved.



Hypercasting™ - Turning the internet into a retail tool. www.muddhypercasting.com
0
joshj
7/24/2007 11:16:29 AM

okies...... 


Prashant
0
prashant_victory
7/24/2007 11:23:50 AM

why wouldn't you mark my reply as answer?



Hypercasting™ - Turning the internet into a retail tool. www.muddhypercasting.com
0
joshj
7/24/2007 12:18:41 PM

I agree, JoshJ's answer looks like it does more to obfuscate the list of includes. Put his function into a file called include.js, and also put into that file the list of other .js files you'd like to include using his function. This code will execute immediately when the HTML page's header downloads and the code will all be imported and useable. It is still possible to see the list, if the user goes through the trouble of downloading and examining include.js, but you cannot immediately see the list by examining the page's source. So at least it does something.

 

 

0
p1000
7/25/2007 12:17:56 AM

joshj:

Here is one way to do it:

Inside your first javascript file, you could have this function:

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}
Then call it with: include_dom("script2.js") . 
This method also allows you to call the script file only when it is needed. Hope this helps.

 

 

Hi Joshj,

Your code is working perfect and its good caz if we call include_dom function then in that case we can access all the  function odf the file which we are including as well as if user will save this page as HTML then he'll not get the javascript file, which normally will save if we give the source of file with script tag using src.

That is very fine.. Wht i have done is i have made a webapplication with one web page and two javascript file. and then i have done as you said. i written your function in javascript file that is in j1.js and the i am including this script file in my page using script tag. and then i am calling your function as i have done below. below is the file which i have coded.

 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AJAX</title>
    <script language="javascript" type="text/javascript" src="j1.js"></script>
    <script language="javascript" type="text/javascript">
    include_dom("j2.js");
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button" onclick="a();" />
    </div>
    </form>
</body>
</html>

  In my j2.js there is only a function name a() is present which displays an alert box,

Now below is the code which i got when i am seeing the siource of the page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
	AJAX
</title>
    <script language="javascript" type="text/javascript" src="j1.js"></script>
    <script language="javascript" type="text/javascript">
    include_dom("j2.js");
    </script>
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTh8MwGw6ulvD1F5IidgKHw+ZCibw==" />
</div>

    <div>
        <input id="Button1" type="button" value="button" onclick="a();" />
    </div>
    </form>
</body>
</html>

  

Now here is the main question. Your code is working fine.

But i want to ask is that Can we hide the filename that is j2.js using any machanism like encryption or anything....... i d't want that user is able to see my file name , now with using ur idea user will nota able to save the javascript file but if he knows the name then he can download my file like below,

www.mywebsite.com/j2.js

that i d't want can we hide this file name when user see the source of the page....????? 


Prashant
0
prashant_victory
7/25/2007 8:26:27 AM

Take include_dom("j2.js"); off of the page and place it INSIDE j1.js - so the home page will look like this (for example, the top line of j1.js would now read include_dom("j2.js"); ): - You might be able to put that call elsewhere in the j1.js file (the more obscure the better of course!) But make sure you don't call anything in that file until it has been instantiated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
	AJAX
</title>
    <script language="javascript" type="text/javascript" src="j1.js"></script>
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTh8MwGw6ulvD1F5IidgKHw+ZCibw==" />
</div>

    <div>
        <input id="Button1" type="button" value="button" onclick="a();" />
    </div>
    </form>
</body>
</html>
 
Also, to make things more obscure, why not write a function like this... This would prevent searches for ".js" from finding the file name and you would call it like this: (Replace deliberatelyObscureName with a name of your choice!)
function deliberatelyObscureName(strString)
{
include_dom(strString + "." + j" + "s");
}
deliberatelyObscureName("j2");
 
0
Sohnee
7/25/2007 11:21:58 AM

The tool that I'm using to include a js file in another js file is JS IncludingEngine. You can write the include statement inside the js file and the engine will load the scripts in the right order

something like this:

in fileC: $include("fileA.js", "fileB.js") // in fileA or B I can have other includes
in html doc: $include("fileC.js");

it also checks circular dependencies. 

and, it's free...

 You can read the article about how it is done at codeproject.com

0
johnrastem
8/1/2007 3:57:03 PM
Reply:

Similar Artilces:

Importing a .js file into a .js file
I have a javascript file that tries to call a function in another javascript file and it doesn't seem to be working.  Is there an 'import' or 'include' statement that I need to have.  The reason I'm not just putting the function in the file is that I want it to be available to a bunch of different javascript files. Thanks just call the function in the external javascript file as usual..the only condition is that both files must be included in the page.Home Is Where the Wind Blowshttp://pushpontech.blogspot.com...

Reference js file from another js file
Hi, Is it possible to reference the functions from one js file in another js file? Any help is greatly appreciated. Thanks, Hey there, Try to include it with the include directive, that is put the following in your js file like <!-- #include file="yourfile.inc"--> really, i didnt try it, but it should work, let me know if it works CheerzThe best glimpse of heaven is on the way into hell ;)yousef.omar@gmail.comhttp://nonstopbiztalk.blogspot.com Assuming that you are using the javascript in a webpage, if you have references to both files then you sh...

Referencing another js file in an external js file
HiI'm trying to do something simple all I need to do is to reference another javascript file inside a javascript file. Is there an "include" statement that I can use.Many thanks no , but you can write document.write('<script src="path" type="text\javascript" ></script>');SHASHANK BHIDECAPGEMINI INDIA"THE ROAD TO SUCCESS IS ALWAYS UNDER CONSTRUCTION MARK MY RESPONSE AS ANSWER TO HELP ME BUILD IT :)"  ok I had a feeling that was the case, no probs and cheers. Hi, Its very simple. Iin the  page where are referring...

referencing .js files from withen another .js file
Hi: How can I include a reference to a js file to use for objects in another js file im looking for the code equivalent to "import" in vb.net, or "include" in c++ etc Thanks a lot, HF As far as I know there is no way to do it. All scripts should be referenced from hosting page and rendered in order of appearance. You can use objects from script already rendered.Leon LangleybenMCSD, ASP.NET MVPBlog I've seen code in .js files that does something like: document.write("<scr" + "ipt language='javascript' src='<Your include file>'></...

Importing External JS files from an External JS file
 Hi Guys,I was trying to figure out a way to include an external js file from with in a js file.  Similar to a Import, or #include in C based languages.  I've tried writing a function that injects a <script> tag into the header of the document.  Here is what I'm trying:In test.js://function for including external *.js filesfunction inc(filename){ var head = document.getElementsByTagName('head').item(0); var script = document.createElement('script'); script.src = filename; script.type = 'text/javascript'; head.appendChil...

how to call js-file from js-file
Hello, I have wrote a FF-extension. The extension consists of a dialog. In the dialog file 'dlg.xul' I import a js-file with '<script type="text/javascript" src="chrome://helloworld/content/ Menue.js"/>' I can now call functions form 'Menue.js' out of the 'dlg.xul' (excellent). Now I want to import the js-file 'Tools.js' in the js-file 'Menue.js'. So I write '<script type="text/javascript" src="chrome://helloworld/ content/Tools.js"/>' at the beginning of the file 'Me...

Include a .JS file into a .JS file
Hi,   Its very simple as below.   For example, Requirement is to add reference in File1.js to File2.js.   document.write("<script type='text/javascript' src='../JS/File2.js'></script>");  All you need to take care is, the source path must be relative to the Page, rather than to the File1.js    Thanks & Regards, Arun Manglick || Senior Tech Lead || http://arun-ts.blogspot.com/  Arun Manglick  Can it not be done like this :include('myFile1.js');  inclu...

How to include a js in another js file
Hello! I'm making a add-on extension and I have a problem to use functions that I defined in a javascript file. I have a directory Data and, in this directory, I have a file math_lib.js and a file cript.js. I'm trying to use the functions of math_lib.js in the cript.js, but this does not work. Someone has some idea? Thanks ...

js file inside js file and syntax error
I have include this .js file inside another .js file like this.... document.write('<scr'+'ipt type="text/javascript" src="scripts/rollover.js" ></scr'+'ipt>'); And I am getting an error from this line and can't see where...     document.write("<td background=images/myImg.jpg'><a href='mailto:webmaster@isp.com' target='_blank' onMouseOver='MM_swapImage('CO e-mail','',images/eImg.jpg',1)' onMouseOut='MM_swapImgRestore()'><img src=images/emailButton.jpg' alt=' CO e-mail' name='CO e-mail' width='157' height='50' border='0'></a></td>"); ...

Include Code-behind .js into another Code-behind .js file
I have a utility code-behind file in .js and I'm trying to add or reference to code in this file from another code-behind file in .js using the "import" feature. Anyone know how? Thanks... ...

Make js file to call some functions in client side
I do not like Java script but some times there is no another choice. In stead of writing Java script line by line every time, I like to create a JS file to store most often used methods and call this file through the RegisterScriptInclude() method. So far, I knew to add Alert("my message") to pop-up message box, can someone tell me where to find more useful Java script methods? Hi here are some examples http://www.4guysfromrolla.com/webtech/code/js.zip http://www.javascript-coder.com/html-form/javascript-form-validation.phtml hope this might help youmk...

how to call javascript function written within js file from server side code (code behind file) ?
Hi, I have js file containing all javascript functions required for my page. I am using ScriptManager.RegisterStartupScript to call javascript from code behind file. But for this i must  specify all javascript code from code behind file only. For eg.  if i use Page.ClientScript.RegisterStartupScript(typeof(System.String), this.ClientID + "_Init", strScript); where strScript contains <script>alert("hi");</script>, on page_Init event, this script is called. But when same script is embeded within a function  and i try to give function name in ...

Wrap js file into dll file
 Hi, I would like to wrap my javascript file (js) into my dll file after compiling. The same like the ajax control toolkit, the javascript files will be added into the dll file and execute automatically before init_page. I already tried to look at the source of ajax control toolkit but I could not follow it ways to implement it. Could you please help me to give me an example? Thanks in advance. here's an example: http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx    Mike Banavige~~~~~~...

including JS file in ascx file
formore then a day i try to include JS file in one of my user controls... i tried: <script src="myjs.js"></script>, anf tried to move the file to the same folde, to another folder, i used "~/foldder/mujs.js"and more and more... i allways get the same error- object required.... does it possible to include the JS file in the html of ascx file? if yes, how i do it? i justcan't find the way.... hi yani, its working here my side with following syntax <script language="javascript" type="text/javascript" src="sourcing.js" ></script> thanks, satish.Kind Attn: If a re...

Web resources about - Importing a .js file in another .js file...... - asp.net.client-side

Viral Channels: Hi5 Adds Contact Importing to App Invite Flow
... social networking platform I’ve seen, hi5 announced tonight that it is adding the ability for users to invite non-hi5 friends to apps by importing ...

YouTube - iPad importing photos from floppy disk.
You are using an outdated browser, which YouTube no longer supports. Since some features on YouTube may not work, you are viewing a lightweight ...

Tony Abbott considers importing US-style school program with private industry involvement
Tony Abbott is considering an unprecedented Americanisation of the school education system with radical changes that could see HECS-style fees ...

Importing fuel will prove better for the consumer
Importing fuel will prove better for the consumer

Sydney grandmother Lorn Cheng arrested for allegedly importing heroin in 252 pellets
Brenda filed a missing persons report last Friday, concerned that her mother hadn't returned from a holiday in Cambodia when she said she would. ...

Importing more fads from failed systems
Fast-tracking talented professionals into the classroom is yet another imported fad from England and the US - both of which are failed education ...

Mark Clermont sentenced to life in jail for importing drugs worth $260 million inside road roller
A judge has praised police for preventing cocaine and methylamphetamine with a street value of $260 million reaching the streets of Sydney and ...

Hong Kong man charged with importing 150kg of crystal methamphetamine into Sydney
A Hong Kong national is charged with importing 150 kilograms of ice following a joint-agency investigation in Sydney.

Man, 91, charged with importing cocaine hidden in soap boxes into Australia
A 91-year-old Sydney man has been charged with importing cocaine hidden in soap into Australia, prompting police to warn travellers to beware ...

Adobe updates Photoshop Express for Android with blemish removal tool, RAW importing, more
Adobe has actively been rolling out new and powerful tools for mobile platforms in recent months including Lightroom Mobile for iPad followed ...

Resources last updated: 1/12/2016 12:05:49 AM