Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

Javascript function will not work in .asp

I have a javascript function that works fine on a page with the extention of .htm but if i take the same page and make it a .asp it does not work. can somene help me with htis as it is frustrating to me. The code in the <head> of the page is as follows:

<script language="Javascript">
function toggle(myId) {

   if (myId.className == "Show")
       myId.className = "NoShow"
   else
      if (myId.className == "NoShow")
          myId.className = "Show"
}
</script>

Open in new window


and my usage of this is as follows:
<p>&nbsp;<div class="menu2" onclick="toggle(show2)" style="cursor: hand;"><p class="CellColorLightest">
                <strong>1. '80's Songs</strong></div><div id="show2" class="NoShow">

Open in new window

0
jlcannon
Asked:
jlcannon
  • 4
  • 4
  • 3
  • +3
1 Solution
 
leakim971PluritechnicianCommented:
do a right click on the page in your web browser when it don't work, choose view source, post it here
0
 
G_HCommented:
Also, Ctrl+F5 (Twice, in the browser) the htm version, to ensure you are not looking at a working but cached version of the page.

GH
0
 
G_HCommented:
GH (ASP Guru, not JS):

Should the lines:
myId.className = "NoShow"

Open in new window

end with a semi-colon...?

GH
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
GaryCommented:
<script type="text/javascript">

language= has been deprecated for years.
0
 
Dave BaldwinFixer of ProblemsCommented:
I put your code in a page and looked at it in Firebug.  It is doing exactly what you're telling it to do.  What is it you think it should be doing?
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If you are running the js SEERVER SIDE and you have your page set for vb script then you need to place your js code inside

<SCRIPT LANGUAGE=VBScript RUNAT=SERVER>
        if (myId.className == "Show")
       myId.className = "NoShow"
   else
      if (myId.className == "NoShow")
          myId.className = "Show"
}
</SCRIPT>

Depending on what you are doing, it is faster to run js client side.  In that case you just need to use <script></script>

If you are not using vbscript in your page, then put this at the very top
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>

Remember, you can pass variables from Server side to client side but not the other way around.
0
 
jlcannonAuthor Commented:
I wish I could paste my source code but i would have to redact 90% due to IP but I look at the source and they are the same. it just seems that the JS does not function in the asp world. I have changed the working from language to type and also have tried both the suggestions from padas and no dice on those either. What it does is basically makes a heading of lets say '80's Music and then when you click on '80's Music it expands to show the links underneath it. click again and it compresses to not show the links and just the title again.
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
We don't want ALL your code anyway.  Props to @Ray_Paseur for this http://sscce.org/ You just need to come up with the very basic code sample.  

Js does work with classic asp.

The only thing that does not work is trying to pass CLIENT SIDE variables to SERVER SIDE.  

Below is a starting point for you to start with.  The first is for serverside  javascript  and the 2nd is for serverside vb script.  You can run serverside js script in the bottom version by placing your code inside of <SCRIPT LANGUAGE=VBScript RUNAT=SERVER> </script>.  You can run client side js code in either.  Again, the client side js can't talk to serverside.  But you can pass serverside to clientside.
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Open in new window

or
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Open in new window

0
 
jlcannonAuthor Commented:
ok I am assuming I am doing it the way it cannot be done since none of the options listed have worked.  The working .htm file and the non working .asp file are identicle with the exception of I added a new menu structure and had to change it from htm to asp so the new template would function correctly.

This is what I did on the original .htm page that still works right now but this does not work on the .asp page at all. so I assume it is client side trying to run server side??


How to make it work?

Add the above routine "toggle" in the HEAD section of your web-page or if you want to use the code on multiple pages, create a separate file ToggleContent.js in the scripts folder of your web-site and include the file into your web-page e.g. like:
<script language="JavaScript" src="/scripts/ToggleContent.js"></script>

Create the Show/Hide link or button (Example)
<div class="in" language="JavaScript" onclick="toggle(someID);">Show/Hide Code</div>

Create the area you want to show/hide (Example)
<div id="someID" class="NoShow">your content</div>

Notes:

The in this example used styles "Show", "NoShow" and "in" are part of the global standard style sheet.
The ID "someID" can be any name as long as it is a unique ID on the page.
0
 
G_HCommented:
I have saved the following as test.ASP and test.HTM

Both work:
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Untitled Document</title>
		<script language="Javascript">
			function toggle(myId) {
				if (myId.className == "Show")
					myId.className = "NoShow"
				else
					if (myId.className == "NoShow")
						myId.className = "Show"
			}
		</script>
		<style>
			.NoShow {display: none;}
			.Show {display: block;}
		</style>
	</head>
	<body>
		<div class="menu2" onclick="toggle(show2)" style="cursor: hand;">
			<p class="CellColorLightest">
				<strong>
					1. '80's Songs
				</strong>
			</p>
			</div>
			<div id="show2" class="NoShow">
				Shown or Hidden
			</div>
		</div>
	</body>
</html>

Open in new window

...Can you please do the same on your server, and let us know what happens?

GH
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It's hard to say what you are doing.  You really need to make page of shortened code of exactly what you are trying to do.  Otherwise, we are just playing guessing games.  The issue probably could have been solved by now....

If you are trying to toggle something on or off once the page loads, that is done client side.
0
 
jlcannonAuthor Commented:
yes both work as asp or htm so it is obviously something in my page. I will post abbreviated code.
0
 
jlcannonAuthor Commented:
GH I see now the issue. in the .htm file it was not needed to put the

<style>
                  .NoShow {display: none;}
                  .Show {display: block;}
            </style>

but in the .asp it is. it now works. thank you for all of yalls great help.
0
 
G_HCommented:
aaah... You need to put your css-file-include into the ASP as well, jst like you did in the htm..!

GH
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now