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:

window.location.href='https://www.example.com/facebook/file-uploading/index.php?fail';

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

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    
     
	$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);
	
	if(move_uploaded_file($file_loc,$folder.$final_file))
	{
		$sql="INSERT INTO facebook(file,type,size,topic,title,description,link) VALUES('$final_file','$file_type','$new_size','$topic','$title','$description','$plink')";
		mysqli_query($link,$sql);
		?>
		<script>
		alert('Successfully Uploaded!');
        window.location.href='index.php?success';
        </script>
		<?php
	}
	else
	{
		?>
		<script>
		alert('Error While Uploading File');
        window.location.href='index.php?fail';
        </script>
		<?php
	}
}
?>

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 example.com/index.php, 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:

https://www.example.com/facebook/file-uploading/upload.php

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:

https://www.example.com/facebook/file-uploading/upload.php

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

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

same thing:

https://www.example.com/facebook/file-uploading/upload.php

If I do it like this:

window.location.href=‘index.php?fail’;

It goes here without the “/”

https://www.example.comfacebook/file-uploading/?fail

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.