Link to home
Start Free TrialLog in
Avatar of fox_statton
fox_statton

asked on

Why is this javscript not working in Chrome?

Hi all,
I have a simple big of JS (shown below), that takes the contents of one textfield, does a bit of converting then writes it to another field.

It works fine in IE, but not in Chrome, can anyone see why?


<script>

function set_filename() {

var filename=document.getElementById('title').value;

filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';

document.getElementById('filename').value=filename;

}

</script>

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

not sure what html code you are using, but this one works for me
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script>

function set_filename() {

var filename=document.getElementById('title').value;

filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';

document.getElementById('filename').value=filename;

}

</script>
</HEAD>

<BODY>
	<input type="text" id="title" onblur="set_filename()"/>
	<input type="text" id="filename"/>
</BODY>
</HTML>

Open in new window

Looks fine to me in chrome

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Page</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function(){
  set_filename();
});
function set_filename() {
var filename=document.getElementById('title').value;
filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';
document.getElementById('filename').value=filename;

}

</script>
</head>

<body>
<input  type="input" id="title" value="My page?"/>
<input  type="input" id="filename"/>
</body>
</html>

Open in new window

screenshot.png
Could you please tell us What's the error / wrong behav. it throws?

The only thing can be better done is to identify the space with its proper class \s .

Try with it and please report the problem.

HTH
Bye!
Yeah, you should be sure to recall the function AFTER the page load, or at least after the creation of the html objects involved.

Since i think you're firing the function onfocus, onblur or while typing in the first field, this is not a problem.

Try replacing / / with /\s/
Avatar of fox_statton
fox_statton

ASKER

Hi all,
When I click on the button that triggers the behaviour, nothing happens in chrome...
Do you think the button code could be wrong:


 <a href="#" onClick="set_filename();"><img src="/wand.gif" border="0" /></a>
make it

 <a href="#javascript:set_filename();"><img src="/wand.gif" border="0" /></a>
Nothing wrong with button code. Place alert in your function to check whether function is calling or not
OR try below code whether it is working in chrome or not.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Page</title>
<link href="form.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function set_filename() {
var filename=document.getElementById('title').value;
filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';
document.getElementById('filename').value=filename;
}

</script>
</head>

<body>
<input  type="input" id="title" value="test My page?"/>
<input  type="input" id="filename"/>
<a href="#" onClick="set_filename();"><img src="/wand.gif" border="0" /></a>
</body>
</html>

Open in new window

Nope, still nothing.
In chrome is not coming up with an error or anything, just not working.
see if this code works for you, since it works for me
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script>

function set_filename() {

var filename=document.getElementById('title').value;

filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';

document.getElementById('filename').value=filename;

}

</script>
</HEAD>

<BODY>
	<input type="text" id="title" onblur="set_filename()"/>
	<input type="text" id="filename"/>

	<br>

	<a href="#javascript:set_filename();">Try it</a>
</BODY>
</HTML>

Open in new window

That works fine.
But when I put it into my other script it stops working.

Is there a way I can debug js in chrome and see all the errors coming up when I click on a button? Still continues to work in IE.
Here is a complete page, which works in IE but not in Chrome....
<html>
<head>
<script>
function set_filename() {
var filename=document.getElementById('title').value;
filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';
document.getElementById('filename').value=filename;

}



</script>



<body bgcolor= "#FFFFFF" text="#000000">

<form action="f.php" method="post" name="form1" target="_parent" id="form1">

     <input type="text" name="title" size="55" value="" > 

 <input name="filename" type="text" id="filename" size="30" value="">

              <a href="#" onClick="set_filename();"><img src="/images/wand.gif" border="0" /></a>
			  </body>
			  </html>

Open in new window

Check this
<html>
<head>
<script>
function set_filename() { 
var filename=document.getElementById('title').value;
filename = filename.replace(/ /img, "_");
filename = filename.replace(/\?/img, "");
filename = filename.toLowerCase()
filename = filename+'.html';
document.getElementById('filename').value=filename;

}



</script>



<body bgcolor= "#FFFFFF" text="#000000">

<form action="f.php" method="post" name="form1" target="_parent" id="form1">

     <input type="text" name="title" id="title" size="55" value="" > 

 <input name="filename" type="text" id="filename" size="30" value="">

              <a href="#" onClick="set_filename();"><img src="/images/wand.gif" border="0" /></a>
        </body>
        </html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sonawanekiran is right.
You forget to put the id attrib.
Can believe I missed that, thanks guys!