+ Reply to Thread
Results 1 to 10 of 10

Thread: How to call a .js file in my form?

  1. #1
    dtz
    dtz is offline
    Barn Regular dtz is an unknown quantity at this point dtz's Avatar
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    3

    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 12:17 PM.

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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?

  3. #3
    dtz
    dtz is offline
    Barn Regular dtz is an unknown quantity at this point dtz's Avatar
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    3

    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

  4. #4
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    hi
    post code of showhide1.js file here.
    Love is physical attraction and mental destruction

  5. #5
    dtz
    dtz is offline
    Barn Regular dtz is an unknown quantity at this point dtz's Avatar
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    3

    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>
    

  6. #6
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    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 01:01 PM. Reason: Post your html code also here so that can run at my end.
    Love is physical attraction and mental destruction

  7. #7
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    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)
    Love is physical attraction and mental destruction

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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.

  9. #9
    Barn Newbie yangski is an unknown quantity at this point yangski's Avatar
    Join Date
    Jul 2009
    Posts
    26
    Rep Power
    3

    hi,

    can you post here your asp code that's calling the function from your .js file? thanks....

  10. #10
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    795
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    .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.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO