SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to include java in a js file

    Hi all,
    I have the following script in my jsp. I want to cut this script and include it in a .js file But it is not working with me.

    Here is the script :
    Code:
    <script language="javascript">
     
    function checkForUnaddeditems() {
    		
       var mainElement = document.forms.mainentry    
       var quatityValue   = mainElement.<%=quantityelement%>.value
        var productIDValue = mainElement.<%=productIDelement%>.value
        var price  = "";
        
        if (<%=item.hasPrice()%>) 
                price = mainElement.<%=priceElement%>.value;
    
        if (quatityValue.trim() == "" || !isNumeric(quatityValue) 
               || productIDValue.trim() == "" || unitValue.trim() == "" ) { 
        	   return false;
        	} else if (<%=item.hasPrice()%> && price.trim() == "") 
        	{    		
        	  return false;
        	} else {
        	    return true;
        	}
    }
    
    </script>
    I copied the previous code into a check.js file and referred to it in the jsp by
    Code:
    <SCRIPT type="text/javascript" SRC="check.js"></SCRIPT>
    I'll be grateful for any suggestion and idea?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would need to make your webserver parse this file as jsp. Maybe changing the file extension to jsp would do it. But, you will need to make sure those variables exist too. You might consider taking that jsp out of the function, and using jsp only to define the arguments to the function.

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You would need to make your webserver parse this file as jsp. Maybe changing the file extension to jsp would do it. But, you will need to make sure those variables exist too. You might consider taking that jsp out of the function, and using jsp only to define the arguments to the function.

    Thanks crmalibu for you reply,

    Well When I put pure javascript into js file it works fine, but when I add vars e.g. (<%=item.hasPrice()%>) IT STOP working.

    I am not sure if I can put (<%=item.hasPrice()%>) in js files, or I am still missing things,

    Again thanks for you reply

  4. #4
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A simple solution would be for the javascript function two accept parameters, since in your case you need a whole bunch of values in the javascript, you would need to pass them all or embed the javascript in the jsp.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript and jsp are completely different things, despite the similar names. jsp is a serverside programming language. javascript is clientside.

    It's important that you understand the difference.
    http://www.sitepoint.com/article/adv...design-primer/

  6. #6
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Javascript and jsp are completely different things, despite the similar names. jsp is a serverside programming language. javascript is clientside.

    It's important that you understand the difference.
    crmalibu, thank alot for the link,

    DO you have any suggestion for my problem???


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
  •