Hi All,

I had a issue with converting an inline js function to external file , I know it is pretty dumb question, But would really appreciate if you guys can help me in solving it.

Steps - I cut the script function from a file and pasted in a newly created .js file.

and then i added a reference -
<script type="text/javascript" src="/test/js/my.js"></script>

My myscript contains a var shareMenu which is actully used in following.

External Script Contents - There is no script tag
var shareMenu = new Array();
shareMenu[0] = '<a target="_blank" href=.....>


I am using using my shareMenu var in page as -
<a href="##" rel="nofollow" onClick="return dropdownmenu(this, event, shareMenu, '150')" onMouseout="delayhidemenu()">

Whenever i debug - it gives me error - shareMenu undefined

To solve this i added a path in <a target ="/test/js/my.js".. > But it doesnt work as well. Is there any other way to use the external js var ?

Please suggest with some ideas.

You don't need to add a path in the anchor tag all target does is specify if the link should open in a new window or same window (if you had an href to open to defined). Can you verify that the browser is loading your external script? If the src attribute is wrong and the browser doesn't see the script file your var shareMenu won't exist. If in IE try opening up developer tools. Then click on the tab that says Script. To the right of the Start Debugging button there should be a drop down that has a list of files (will be your html file and hopefully your script file "my.js"). If you do not see my.js in that drop down your script isn't being loaded by the browser. You can do the same as above in FF using Firebug and it's pretty much the same directions. What is the path of your html file? If test/js/my.js isn't in the same directory level your browser won't find it. For example if your html file is at: c:/code/myHTML.html then your js should be at c:/code/test/js/my.js.

Thanks for your reply.
Need to check/verfiy 2 things//
1 ) So do you mean - my javascript var shareMenu would be accesible if my js is properly referenced ? I would not have to add any other referencing code.
2)And my js reference script - <script type="text/javascript" src="/test/js/my.js"></script> i have added in the head tag of my page. That should be fine as well

You are right. I cant see my file in the IE->DeveloperTools->Scripts->Dropdown arrow as you suggested. But i am wondering , the code i commented is still commented in the file on browser but the script reference code - <script type="text/javascript" src="/test/js/my.js"></script> and the file itself does not exist in browser listings too.
Can you please suggest some way i can add my file in the browsers file and thereby convert my inline script to external js.

Would Really appreciate for your quick response.
Thanks

For 1. and 2. Yes if your js was referenced properly in the <script> tag you put in the head of the document all should be well. I just noticed that in my example I had placed a "/". This won't work if the test directory is in the same dir as your html file. Remove it and all should be well.

script type="text/javascript" src="test/js/my.js"></script>

. My apologies for that. When you have the "/" in front it is telling the browser to look for a directory at the root rather than current directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.