Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code won't get called! Arrrggg!!!

Status
Not open for further replies.

ppetree

Programmer
Mar 3, 2007
60
US
I've screwed around with this for two hours and can't get this javascript function to get called! I must be missing something here! Extra eyes would be GREATLY appreciated!

Here is my html code:
Code:
<html>
<head>
<link rel="StyleSheet" href="/javascript/coolstyle.css" type="text/css">

<script language="JavaScript" type="text/javascript" src="/javascript/pop-closeup.js"></script>
<script language="JavaScript" type="text/javascript" src="/javascript/javascripts.js"></script>
<script language="JavaScript" type="text/javascript" src="/javascript/clipboard.js"></script>
</head>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<center>
To use the following code, simply cut and paste into<br>
your html document exactly as you see it here.<br>
<br>
<textarea name="html_code" cols="60" rows="6">Cut and Copy this Text
</textarea>
<button onclick="javascript:copyToClipboard(document.getElementById('html_code').value);">Copy To Clipboard</button>
<button onclick="javascript:copyToAlert('Hello World!');">Copy To Alert</button>
<button onclick="alert('Hello World!');">This works fine!</button>
</center>
</body>
</html>

Here is my javascript:clipboard.js file:
Code:
function copyToAlert(s)
{
  alert("Text to Copy: \n" +s);
}

function copyToClipboard(s)
{
  alert("Hello World!");  

  if( window.clipboardData && clipboardData.setData )
  {
    clipboardData.setData("Text", s);
  }
  else
  {
    .
    .
    .
  }
}
 
I see nothing with an ID of html_code.


I see this textarea:

<textarea [!]name[/!]="html_code"

and this call to your code:

onclick="javascript:copyToClipboard(document.getElementBy[!]Id[/!]('html_code').value);">

Give your textarea an ID of html_code.

[monkey][snake] <.
 
Trollacious, I've never downloaded firefox... too deep into this project to load a new browser onto my system... even worse we're a die hard M$ shop so mgt really doesnt care if we don't render in any browser other than IE.

Monksnake, as for adding an id, not necessary because the following code works just fine:
Code:
<button onclick="alert(document.getElementById('html_code').value);">Show text in Alert</button>

I put a block of code in the js file called copyToAlert and have played around with calling it in various forms including:
Code:
<button onclick="copyToAlert('Hello World!');">copy to Alert</button>

Problem is the code is just not getting called! I have used this same technique about 1000 times on this site and it works everywhere else... me thinks me hath done something stupid and me can't see it! LOL

 
too deep into this project to load a new browser onto my system

It's sound advice, though. Firefox has a good JavaScript console - far better than anything IE has... and it will probably show you immediately where the error is.

If you take 5 mins out to install it, it could save you hours.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Some other things to check:

- Check that you have the right path & filename to all your JS files

- Check you don't have duplicate functions called "copyToAlert" (even over different files)

- Check you don't have "<script>" tags in your external JS files

- Remove the "javascript:" from all of your "onclick" attributes - it simply isn't needed.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I'd guess it's a path error related to where the external JS file is.

Lee
 
Paths are correct, I have two other .js files that included just above this one and they work just fine:

Code:
<script language="JavaScript" type="text/javascript" src="/javascript/pop-closeup.js"></script>
<script language="JavaScript" type="text/javascript" src="/javascript/javascripts.js"></script>
<script language="JavaScript" type="text/javascript" src="/javascript/clipboard.js"></script>

Filename and function names have been checked by me and one other and are exactly as called.

Did find a </script> tag at the bottom of the clipboard.js file, removed that no change.

I also removed the javascript: script identifier with no results. I will say though that I have other pages that will not work unless the function name is preceeded by the javascript:
 
In the amount of time you've spent messing around with your code, you could have downloaded Firefox, installed it, viewed the page with it, and checked the Javascript error console to see what the problem is. It's a fantastic tool for finding JS errors.

Lee
 
You can also temporarily remove the other external JS files from the HTML to see if anything in them is causing the problems.

Lee
 
I will say though that I have other pages that will not work unless the function name is preceeded by the javascript:

They're probably where you are calling JS from and anchor's HREF attribute - which is a different situation entirely from an onclick attribute.

If you're unwilling to help yourself by installing Firefox, perhaps you could provide a URL to the page so everyone here who does have Firefox can visit it and probably give you an answer in seconds?

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I don't know if this is relevant but we've had lots of problems getting drag and drop working in XP here.

Nothing wrong with the code - it worked fine in W2K (slightly earlier version of IE - that might make a difference) but when we migrated to XP it all just stopped working.

We ended up rewriting it so that we used our own drag and drop (trapping mouse events) instead of the Microsoft way.

It may be the same with setData, which would mean that your code is fine, but something else is causing it to fail.

Do you need to cut and paste like this?

Also, finally, on the subject of Firefox's debugging abilities, I agree they're good, but nowhere near as fine as using Visual Studio for debugging javascript. Not even close. Step through scripts, immediate window, breakpoints, everything. Ace - couldn't work without it.

Chaz

 
Not even close. Step through scripts, immediate window, breakpoints, everything

Until recently, I would have agreed with you. With the recent update to Firebug (around 3 weeks ago), the debugging abilities available to Fx users have inceased to the same level as VS, in my opinion.

Now IE and Fx debugging capabilities are pretty much equal.

Dan.



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok... I installed Firefox, logged in to the offending area, loaded the offending page, opened the Firefox Error window and it was... BLANK!!!

(Actually it had some .css errors but the page I'm calling doesn't use a .css file so I deleted those errors and loaded and reloaded the offening page and it generated not one single error.

I opened the Firefox DOM inspector, was able to see my javascript file, it found it etc.

The onClick is simply not firing. I added a <form> tag and that didn't help. I went from <button> tag to <input type="button" and that didn't help...

sigh...

Any other ideas?

If anyone wants to take a peek go to:
 
I just copied and pasted your code into an HTML file and clipboard.js, changed the path of the JS file to the same folder as the HTML file, and it worked fine.

The path you have for the external JS files is root directory/javascript (server or local, depending on the environment). If you're viewing this locally (not through a web server), do you have a C:\javascript folder? If you're viewing the file with a URL that starts with http, is there a javascript directory off the root directory you're using?

Please copy and paste the URL of that page in here from the browser address bar.

Lee
 
Try using the absolute URL and see what happens.
Code:
<script type="text/javascript" src="[URL unfurl="true"]http://www.serviance.com/javascript/pop-closeup.js"></script>[/URL]
<script type="text/javascript" src="[URL unfurl="true"]http://www.serviance.com/javascript/javascripts.js"></script>[/URL]
<script type="text/javascript" src="[URL unfurl="true"]http://www.serviance.com/javascript/clipboard.js"></script>[/URL]

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top