Solved

Javascript function will not work in .asp

Posted on 2013-06-04
14
459 Views
Last Modified: 2013-06-04
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
Comment
Question by:jlcannon
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39219444
do a right click on the page in your web browser when it don't work, choose view source, post it here
0
 
LVL 11

Expert Comment

by:G_H
ID: 39219481
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
 
LVL 11

Expert Comment

by:G_H
ID: 39219484
GH (ASP Guru, not JS):

Should the lines:
myId.className = "NoShow"

Open in new window

end with a semi-colon...?

GH
0
 
LVL 58

Expert Comment

by:Gary
ID: 39219507
<script type="text/javascript">

language= has been deprecated for years.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39219550
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39219694
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
 

Author Comment

by:jlcannon
ID: 39219868
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39219905
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
 

Author Comment

by:jlcannon
ID: 39220019
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
 
LVL 11

Accepted Solution

by:
G_H earned 500 total points
ID: 39220086
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39220096
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
 

Author Comment

by:jlcannon
ID: 39220108
yes both work as asp or htm so it is obviously something in my page. I will post abbreviated code.
0
 

Author Closing Comment

by:jlcannon
ID: 39220127
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
 
LVL 11

Expert Comment

by:G_H
ID: 39220242
aaah... You need to put your css-file-include into the ASP as well, jst like you did in the htm..!

GH
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now