Hello, I have a script that like;

<script language="JavaScript">
document.write('<input border=0 type=button value=\'\' ');
document.write(' onClick = "location.replace(');
document.write(' document.stats.site.value + \'/\');"');
document.write(' value="Search">');
document.write('</input>');
</script>

Works nice. It directs the user to the location that written in the text input. I mean user write a site and the script directs the user to mysite.com/stat/userwrittensite.com/

The problem is,

If the user do the same action after, it directs
to mysite.com/stat/userwrittensite.com/userwrittensite.com2/

How can I fix it ? Thanks...

Recommended Answers

All 16 Replies

document.write(' document.stats.site.value + \'/\');"');

can u tell me why u ' \'/\');' in end of line..?? :O

Try this one out:

<script type="text/javascript">
<!--

window.onload = function() {
   if ( window.status === "activated" ) {
   return false; 
   } window.status = "activated";
 var content = "";
   content += "<div>\n";
   content += '<button onclick="location.href = location.href.replace( document.stats.site.value, document.stats.site.value + \'/\' );"'; 
   content += ">Search</button>\n";
   content += "</div>\n";

   (( "writeln" in document ) ? document.writeln( content ) : document.write( content ) );
}
// -->
</script>

Try this one out:

<script type="text/javascript">
<!--

window.onload = function() {
   if ( window.status === "activated" ) {
   return false; 
   } window.status = "activated";
 var content = "";
   content += "<div>\n";
   content += '<button onclick="location.href = location.href.replace( document.stats.site.value, document.stats.site.value + \'/\' );"'; 
   content += ">Search</button>\n";
   content += "</div>\n";

   (( "writeln" in document ) ? document.writeln( content ) : document.write( content ) );
}
// -->
</script>

I replace with my old submit button, but when you open the page, it opens a page that only the button appears. Here is my page

http://www.downloadic.com/website-rank/index.php

You'll have to rearrange the replaced value of the window location location.replace( /* What should be next? */ ); i just copied the whole lines from your posted code.

Ok, let's bring back the old lines and put some window.status = "activated"; statement into it.

<head>
<script type="text/javascript">
<!--
window.onload = function() {
   if ( window.status === "activated" ) {
   return false;
   } 
 var content = "";
   content += '<button id="btn" onclick="'+
   content += 'location.replace( document.stats.site.value + \'/\');"';
   content += '>Search</button>';
   document.write( content );
   window.status = "activated";
};
// -->
</script>

Try to see if it gets what you really need.

Thanks, but, I tried that but the button doesnt appear on the page.... :S

Once it's casted the remaining portion of the script outside the

if( window.status === "activated" ) { /*Codes to be executed */ } 

// Codes to be halted once the location has been altered.

so if you need something that you want to execute after page redirection must be inside the if() block statement.

Thanks but I don't understand what I should do.

I have a one text input, and the script you wrote. When user write site and submit, it should direct to mysite.com/userssite.com/ and if user write another site and submit again, it shouldn't be like mysite.com/userssite.com/userssite2.com/

So what should I put in the if statement instead of return false; ??

Ok you can try something like this:

<head>
<script type="text/javascript">
<!--
window.onload = function() {

var loc = location.replace( document.stats.site.value + '/' );

var content = "";
content += '<button id="btn" name="btn" onclick="';

   if ( window.status === loc ) {
   content += 'location =' + String( window.status + document.stats.site.value ) + ';">Search</button>';
   } else {
   content += 'location.replace(' + String( loc ) + ');\">Search</button>';
   window.status = loc;
   }
 document.write( content );
};
// -->
</script>

hope this one make it...

essential

Just post your entire code and i'll see what i can do to it.

Ok,

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" name="stats">
<TABLE border=0 bgcolor=#efefef align=center cellSpacing=0 cellPadding=0 border=0 width="750" bgcolor=#fbfbfd>
<TR height=50>
<TD align=right>
<input type=text name="site" value="www.">
</TD>
<TD valign=bottom>

<script type="text/javascript">
<!--
window.onload = function() {

var loc = location.replace( document.stats.site.value + '/' );

var content = "";
content += '<button id="btn" name="btn" onclick="';

   if ( window.status === loc ) {
   content += 'location =' + String( window.status + document.stats.site.value ) + ';">Search</button>';
   } else {
   content += 'location.replace(' + String( loc ) + ');\">Search</button>';
   window.status = loc;
   }
 document.write( content );
};
// -->
</script>

</td>
</tr>
</table>
</form>

The problem is, when I use the code, it directs a new page and only the button appears...

All I want is, if user write the site again, it shouldnt like mysite.com/userssite1.com/userssite2.com/

Ok i'l be right back with you in a bit, just hold on for sec, while im fixing your code.

Thanks... I am sorry if I get you busy.

It's more appropriate if we will provide a static button in the page and attach some function into it.

So here's everything:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Window-target" content="_top">
<title>Free Live Help!</title>
<script type="text/javascript">
<!--

var redirect = function() {
    if ( window.status === "activated" ) {
    location += "/" + document.stats.site.value;
    return true;
    } 
    window.status = "activated";
    location.replace( document.stats.site.value + "/" );
}
//-->
</script>
</head>
<body>
<div id="main">
<form id="stats" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" name="stats">
<TABLE border=0 bgcolor=#efefef align=center cellSpacing=0 cellPadding=0 border=0 width="750" bgcolor=#fbfbfd>
<TR height=50>
<TD align=right>
<input type=text name="site" value="">
</TD>
<TD valign=bottom>

<input type="button" onclick="redirect();" value="Search">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.