Can't get window.location.href to work correctly

I have this file (below) after submitting it gives an alert then suppose to redirect. The problem is the redirect url is missing the trailing slash “/” after .com

So the url looks like this: https://www.example.comfacebook/file-uploading/?fail

I tried many attempts to get it working but even if I add the url like this:


It still does the same thing! How can that be?

include_once 'dbconfig.php';
	$file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
	$file_size = $_FILES['file']['size'];
	$file_type = $_FILES['file']['type'];
	$folder = "facebook_autopost/";

	$topic = $_POST['topic'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $plink = $_POST['link'];
	// new file size in KB
	$new_size = $file_size/1024;  
	// new file size in KB
	// make file name in lower case
	$new_file_name = strtolower($file);
	// make file name in lower case
	$final_file=str_replace(' ','-',$new_file_name);
		$sql="INSERT INTO facebook(file,type,size,topic,title,description,link) VALUES('$final_file','$file_type','$new_size','$topic','$title','$description','$plink')";
		alert('Successfully Uploaded!');
		alert('Error While Uploading File');

Thank you

Hey there!
location = '?success' would be enough I believe.
I have no idea why the / between the url and the pathname is removed in your case. if you set the location this way, it will be added, not replaced entirely. So if you are already on, this should work perfectly.

It still does the same thing. Maybe it’s something else altogether?

what does alert(location) say? Does it already strip the slash?
If you can’t find a way to fix it. you could still go with location.href.split('.com').join('/'). Javascript can’t point you to a complete different url, if you only provide query params or file name.

alert(location) says this:

upload.php isn’t really suppose to be there. I think it is replace. I tried:

location.href.split(‘.com’).join(‘/’) = ‘index.php?fail’

and it redirects to:

If I do it without the index.php part like this:

location.href.split(‘.com’).join(‘/’) = ‘?fail’

same thing:

If I do it like this:


It goes here without the “/”


But if I add the “/” to the url it works.

Only setting location.href directly will take effect. So if you modify the href using split and join, you will have to set it again. location.href = location.href.split('.com').join('/')

However, I would still want to try to find out why the slash is removed.

I’m not sure what you mean that I would have to set it again?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.