DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

How to call a .js file in my form?

This is a discussion on How to call a .js file in my form? within the JavaScript Programming forums, part of the Programming & Scripting category; Hi everyone, i have a show/hide(showhide1.js) file that contains javascript function in it. Each function is for a specific drop ...

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

  #1  
Old March 17th, 2009, 11:14 AM
dtz dtz is offline
Barn Regular
 
Join Date: Feb 2009
Posts: 94
Rep Power: 2
dtz is an unknown quantity at this point
Default How to call a .js file in my form?

Hi everyone,
i have a show/hide(showhide1.js) file that contains javascript function in it. Each function is for a specific drop down list on a form(myform.asp) and there are many drop down list in my form, instead of writting many funtions in my form i want it to called an external page by using the include feature. I have use this bit in my form but i am not sure what is wrong as i am getting a pop up error message :

Code:
<script type="text/javascript" src="showhide1.js"></script>
All my files are within a folder called folder Project A.It contains myform.asp and showhide1.js.How do i call showhide.js from myform.asp??

Last edited by dtz; March 17th, 2009 at 11:17 AM.
Reply With Quote
  #2  
Old March 17th, 2009, 11:25 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

What is the error message?

When you view the source of the page when loaded in the browser, do you see the <script></script> tag?
__________________
Join the folding team
Reply With Quote
  #3  
Old March 17th, 2009, 11:39 AM
dtz dtz is offline
Barn Regular
 
Join Date: Feb 2009
Posts: 94
Rep Power: 2
dtz is an unknown quantity at this point
Default

Quote:
Originally Posted by richyrich View Post
What is the error message?

When you view the source of the page when loaded in the browser, do you see the <script></script> tag?
Hi,
this i what come up when i try to preview the page:


When i click yes, it opens up but ignores the function from showhide1.js
Reply With Quote
  #4  
Old March 17th, 2009, 11:41 AM
guddu's Avatar
Barn Enthusiast
 
Join Date: Jul 2008
Location: Oxford UK
Posts: 334
Rep Power: 2
guddu is on a distinguished road
Default

hi
post code of showhide1.js file here.
__________________
Love is physical attraction and mental destruction
Reply With Quote
  #5  
Old March 17th, 2009, 11:46 AM
dtz dtz is offline
Barn Regular
 
Join Date: Feb 2009
Posts: 94
Rep Power: 2
dtz is an unknown quantity at this point
Default

Quote:
Originally Posted by guddu View Post
hi
post code of showhide1.js file here.
Hi,
this is my .js file
Code:
<script type="text/javascript"><!--

var lastDiv = "";

function showDiv(divName) {

            // hide last div

            if (lastDiv) {

                        document.getElementById(lastDiv).className = "hiddenDiv";

            }

            //if value of the box is not nothing and an object with that name exists, then change the class

            if (divName && document.getElementById(divName)) {

                        document.getElementById(divName).className = "visibleDiv";

                        lastDiv = divName;

            }

}

//-->





var lastDiv1 = "";

function showDiv1(divName) {

            // hide last div

            if (lastDiv1) {

                        document.getElementById(lastDiv1).className = "hiddenDiv1";

            }

            //if value of the box is not nothing and an object with that name exists, then change the class

            if (divName && document.getElementById(divName)) {

                        document.getElementById(divName).className = "visibleDiv1";

                        lastDiv1 = divName;

            }

}

//-->


</script>

                        <style type="text/css" media="screen"><!--

.hiddenDiv {

            display: none;

            }

.visibleDiv {

            display: block;

            border: 1px grey solid;

            }

 .hiddenDiv1 {

            display: none;

            }

.visibleDiv1 {

            display: block;

            border: 1px grey solid;

            }


-->

</style>
Reply With Quote
  #6  
Old March 17th, 2009, 11:54 AM
guddu's Avatar
Barn Enthusiast
 
Join Date: Jul 2008
Location: Oxford UK
Posts: 334
Rep Power: 2
guddu is on a distinguished road
Default

hi
u are setting lastDiv value blank.u are trying to access element which has id blank.
document.getElementById(lastDiv)

Last edited by guddu; March 17th, 2009 at 12:01 PM. Reason: Post your html code also here so that can run at my end.
Reply With Quote
  #7  
Old March 17th, 2009, 12:11 PM
guddu's Avatar
Barn Enthusiast
 
Join Date: Jul 2008
Location: Oxford UK
Posts: 334
Rep Power: 2
guddu is on a distinguished road
Default

hi
i tried your above code.i m not getting any syntax error.may be other part of code has problem.can u post your html code.(code which is calling this javascript functions)
Reply With Quote
  #8  
Old March 17th, 2009, 12:16 PM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

You shouldn't have <script> </script> tags in an external js file. Try removing them

Also, why do you have styles in a js file? They should be in a css file.
Reply With Quote
  #9  
Old July 26th, 2009, 08:17 PM
Barn Newbie
 
Join Date: Jul 2009
Posts: 23
Rep Power: 1
yangski is an unknown quantity at this point
Default

hi,

can you post here your asp code that's calling the function from your .js file? thanks....
Reply With Quote
  #10  
Old July 28th, 2009, 06:28 AM
Shadow Wizard's Avatar
Ask Me About Dragons :D
 
Join Date: Jul 2008
Posts: 54
Blog Entries: 2
Rep Power: 2
Shadow Wizard will become famous soon enough
Default

.JS files can include only script, nothing more nothing less.
having HTML inside .JS files is not possible.
when you have this:
Code:
<script type="text/javascript" src="showhide1.js"></script>
the file being "included" is "compiled" by the browser so it must be valid JavaScript.
for example, <B> is not valid JavaScript command.


to include HTML contents you must use <iframe> tags, or server side code.

Edit: sorry, noticed just now it's old thread... anyway, the information above might still be helpful for others.

Comments on this post
micky agrees: Thanked Post
jmurrayhead agrees: Thanked Post
Reply With Quote
The Following 2 Users Say Thank You to Shadow Wizard For This Useful Post:
jmurrayhead (July 28th, 2009), micky (July 28th, 2009)
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:23 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2