window.location.replace() not working [duplicate] - javascript

I want to navigate to a URL on my site. This is what I have:
var TheDemoURL = window.location.host;
if (SomeCondition1) { TheDemoURL = TheDemoURL + '/fr/demo'; }
if (SomeCondition2) { TheDemoURL = TheDemoURL + '/de/demo'; }
...
window.location.replace(TheDemoURL);
Initially, in the variable watch, I have TheDemoURL: "localhost:49173" and when I alert the final TheDemoURL is looks a good URL but in reality nothing happens.
Why is this not working?

Ok, for those who come here, the solution was to add this:
var TheDemoURL = window.location.protocol + '//' + window.location.host;
Not sure if this is specific to asp.net but it made it work.

Try using
self.location = TheDemoURL;
This will take into account iframes and other weirdness.

Related

Unable to call route through javascript function

I am trying to call a laravel route through javascript function. I have explored all the way on internet and over here but I am not finding up my solution. I referred to one answer over stack overflow about how to call route in javascript function but it didn't work for me. My route:
Route::get("edit_contact/{id}",'ContactController#edit');
Javascript function:
{
var url = '{{ route("edit_contact",":id") }}';
url = url.replace(':id', count);
document.location.href = url;
}
count is defined up in javascript which I need to pass in route.
Issue is this that it is continuously giving error..
Route [edit_contact] not defined.
All these routes are working fine when hitting through html. I have also tried to call up other routes as well through this, but none is working.
Name your route like this:
Route::get("edit_contact/{id}",'ContactController#edit')->name('edit_contact');
var routeUrl = route('posts.index');
var routeUrl = route('posts.show', {post: 1337});
// Returns results from /posts
return axios.get(route('posts.index'))
.then((response) => {
return response.data;
});
Try
let count=3
document.location.href = `edit_contact/${count}`;
try this piece. it should work:
Route::get("edit_contact/{id}",'ContactController#edit');
javascript code
<script type="text/javascript">
$("#valueid").change(function(e){
var id= e.target.value; // the id to be pass
//alert(id);
//checking the state of the domain if secured or not
if ("https:" == document.location.protocol) {
//alert('secured');
location.href="https://www.example.com/edit_contact/"+id;
} else {
//alert('not secured');
location.href="http://www.example.com/edit_contact/"+id;
}
});
</script>
In route file: (web.php)
Route::get("edit_contact/{id}",'ContactController#edit');
In js code:
Here I used count value 3 just for an example. You can use your actual value of count.
<script>
var count = 3;
var baseurl = window.location.protocol + "//" + window.location.host;
var url = baseurl + '/edit_contact/' + count;
document.location.href = url;
</script>

How to get Request.ApplicationPath in jQuery

On my razor view, I am wanting to put all of my jQuery into a separate JS file and then reference that file on the view with #Scripts.Render.
On my view, I have a section jQuery that looks like this:
var settings = {};
settings.baseUri = '#Request.ApplicationPath';
var submissionUrl = "";
if (settings.baseUri === "/ProjectNameOnServer") {
submissionUrl = settings.baseUri + "/api/apiControllerName/apiControllerMethodName/";
} else {
submissionUrl = settings.baseUri + "api/apiControllerName/apiControllerMethodName/";
}
This section of code allows me to test submitting forms with api on both my localhost and live on the server without me having to change the url.
If I am testing on my localhost, then #Request.ApplicationPath == /, but if I test on my server then #Request.ApplicationPath == /ProjectNameOnServer.
I was wondering how I could achieve getting these same values but without the use of Razor, and instead only with jQuery.
I have tried:
var settings = {};
settings.baseUri = window.location.protocol + "//" + window.location.host;
but then settings.baseUri == http://localhost:xxxxx. I just need exactly what #Request.ApplicationPath is giving me.
Use location.host to conditionally create relative path
settings.baseUri = location.host.includes('localhost') ? '/' : '/ProjectNameOnServer';

Reuse javascript code on client side (node.js, express)

I'm still new to Node.js, but I'll try to explain my problem as good as I can.
So I'm working on a movie site to practice my node.js/express skills a bit and I use the following elements (img) on basically every page of my website:
Header with stats and search input and a navigation bar (will be reused on every page)
The follow JS-code are two examples of actions on the client side that I use to navigate to other web pages, this then activates GET on the client side.
$(function () {
$('button').click(function (event) {
event.preventDefault()
// the value people enter in the search box
var search = $('.searchInput').val();
//replace spaces with _
var res = search.replace(/\s/g, "_");
//build the URL
var link = window.location.protocol + '//' + window.location.host + '/search/' + res;
// redirect to trigger GET in indexjs with specific search value
window.location.replace(link);
});
$('.lists').click(function (event) {
var link = window.location.protocol + '//' + window.location.host + '/lists/topAll';
window.location.replace(link);
})
});
I want this to be the same code on every page. I could type the same code every time, but that would be a waste of time.For HTML and CSS I am able to use templates (HTML) or import other CSS files to save time. Is there something similar for JS on the client side?
Put that code in a file, for example "navigator.js" and then load it in your html header in every page you want to use it
navigator.js:
$(function () {
$('button').click(function (event) {
event.preventDefault()
// the value people enter in the search box
var search = $('.searchInput').val();
//replace spaces with _
var res = search.replace(/\s/g, "_");
//build the URL
var link = window.location.protocol + '//' + window.location.host + '/search/' + res;
// redirect to trigger GET in indexjs with specific search value
window.location.replace(link);
});
$('.lists').click(function (event) {
var link = window.location.protocol + '//' + window.location.host + '/lists/topAll';
window.location.replace(link);
})
});
index.html
<script src="navigator.js"></script>
Finally i suggest you to assign an id to your button, for example "searchButton" instead only "button"
Hope this helps

Redirect from master page to its content pages using jquery not working

I called this method on keyUp event of enter key
function PerformSearch(search_key) {
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
//GoToLocation(url);
//window.open(url);
//$(location).attr("href", url);
//window.location.replace(url);
window.location.href = url;
}
use like this
window.location = url;
For your reference read this
You have string concatenation errors in your url.
This:
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
Should be this:
var url = "<%=ResolveUrl('/Test/TestPage.aspx?type=search&search_key=')%>" + search_key;

window.location.replace is not working

function setInit(){
iu.init({
saveFile : 'store/saveImageFile'
,CallBackOnSave : function (s){
console.log("redirect");
window.location.replace("store/details");
}
});
}
iu is an image upload module which has no issues. In fact I can see the console log of redirect.
problem is, the same page gets reloaded, instead of going to the url in replace.
tried other ways like href as well, sadly the same result.
Just use:
window.location.href = "store/details";
Maybe try:
parent.window.location.href = "store/details";
You can use by this way,
Syntax
window.location.href = window.location.host + relativePath
window.location.href = relativePath
example
window.location.href = "store/details";
window.location.href = window.location.host + 'store/details';

Categories

Resources