how to change browser URL without page referesh in Mozilla - javascript

I use the code below but the page still refreshes.
I'm trying to change the url or the page without refreshing it.
//getting the url from browser
var uri = window.location.toString();
//checking whether the url has anything after '?'
if (uri.indexOf("?") > 0)
//then take the url before '?'
var clean_uri = uri.substring(0, uri.indexOf("?"));
//And push this to browser
window.history.replaceState({}, document.title, clean_uri);

history.replaceState should not refresh the page. If page is reloading then you have something else in the code, like, for example, form submission. preventDefault() method can help you cancel the event.
<form>
<button id="bt" type="submit">Submit</button>
</form>
//getting the url from browser
var uri = window.location.toString();
var bt = document.getElementById("bt");
//checking whether the url has anything after "?"
if (uri.indexOf("?") > 0) {
//then take the url before "?"
var clean_uri = uri.substring(0, uri.indexOf("?"));
bt.addEventListener("click", function (e) {
e.preventDefault(); // If you remove this line, the page will refresh and the form will be submitted.
history.replaceState({}, document.title, clean_uri);
});
}

Related

How to redirect to another page through JS (sending parameters)

I'm trying to redirect to another page through JS(sending parameters)
I made the following code to that purpose, it does redirect and the requested page loads up 'ok'
window.location.replace(`http://10.0.30.11:3000/ProteseWEB/pages/detail/${path}?id=${param}`)
The problem is: When the requested page opens, it display an 404 not found on the network tab.
I don't understand why this is occuring, since the page I want is loaded 'perfectly'
The error on the network tab:
Edit(1)
#jlemley and #James Thank you for your answer!!!
Indeed the 404 request and the URL that I requested were different. So I changed the function a little bit.
Here's the code:
openPageDetail = function (path, params, method = 'POST') {
let iplocal = 'http://10.0.30.11:3000/ProteseWEB/pages/detalhe/'
const form = document.createElement('form');
form.method = method;
form.action = iplocal + path;
for (const key in params) {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.type = 'hidden';
hiddenField.name = key;
hiddenField.value = params[key];
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
I tried to make it as a form submit, still, the error occurs
In addition, there's how I call for this function.
I have a DataTable and when the user double click a row, it opens up a detail page for the desired record.
$('.table').on('dblclick', function(e) {
if ((!(e.target.classList.contains('sorting_asc'))) && (!(e.target.classList.contains('sorting_desc')))) {
openPageDetail('contas-a-pagar', {
id: e.target.parentElement.id
});
}
});

jquery find and replace iframe src

I need to load a url in an iframe on page load (which is working), then grab the whole url of that loaded iframe, then once the url is saved as a variable i want to find any text between "target" and "/" (example url http://www.example.com/user/target09576/index.php i wish to find the 09576) then redirect the whole page using the found text (09576) as part of the redirect url.
I know, bad explanation and not a great solution, but any help would be appreciated.
$(document).ready(function() {
var iframe = $('#wmif');
var url = '<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
var searchurl = iframe.contentWindow.location;
var rep = /(.*target\s+)(.*)(\s+\/.*)/;
var target = searchurl.replace(rep, '$2');
window.location = 'http://example.com/go/index.php?target='+target;
});
well the iframe loads the new location ok, but it doesn't do any of the rest.
I would do it similar to the below, same result but with jQuery and an updated regex
$(document).ready(function () {
var iframe = $('#wmif');
var url = ''<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
//var searchurl = iframe.contentWindow.location; <- Invalid
console.log($('#wmif').contents().get(0).location);
var searchurl = $('#wmif').contents().get(0).location.href;
console.log(searchurl);
var rep = /.*\/target(\w+)\/.*/;
var target = searchurl.replace(rep, '$1');
console.log(target);
window.location = 'http://domain.com/go/index.php?target=' + target;
});
Yet another solution
var matches = /target\s?([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
or
var matches = /(?:target\s?)([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
console.log(matches[1]); // 09576
Modify regex to this /(?:target)([^\/]+)/ if you don't expect a space between target and the number.

Edit URL before redirect

Here I am stuck with a scenario where I want to edit href of anchor tag before redirect. My scenario is if href attribute of anchor tag do not contain QueryString then append previous page query string to href means, suppose my current page url is xyz?x=2&y=4
Current href
href="local/services"
After Edit
href="local/services?x=2&y=4"
I tried.
HTML:
test link
JavaScript
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
obj.setAttribute('href') = url + window.location.search;
}
also tried
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
window.location.href = url + window.location.search;
}
but no success.
Please suggest a solution.
Thanks.
The parameters for 'setAttribute' are incorrect.
element.setAttribute(name, value);
Courtesy MDN : Element.setAttribute
Jed Burke pointed to your mistake. So you should use something like this:
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1) obj.setAttribute('href', url + window.location.search);
}
or even simpler:
function appendString(obj) {
if (obj.href.indexOf('?') != -1) obj.href += window.location.search;
}
You can use JQuery for this:
$('.myLink').click(function() {
$(this).attr("href", this.href + "?param=1");
});

how to change in below url redirect script

I have a url redirect script that works very well, but i want some change in it.
when i redirect outgoing link then at the end of redirected url i got something like '#lcinside'
example
http://dl.dropbox.com/u/36139836/unknown.html?urlc=http://imgur.com/
goes to
http://imgur.com/#lcinside
but i want to put #lcinside before url start.
example
#lcinsidehttp://imgur.com
i want to put adf url redirect link instead of #lcinside.
how can i put #lcinside before url .
below is script.
please help me.
<script>
function gup(sName)
{
sName = sName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var sRegEx = "[\\?&]" + sName + "=([^&#]*)";
var regEx = new RegExp(sRegEx);
var aResult = regEx.exec(window.location.href);
if(aResult == null)
{
return "";
}
else
{
return aResult[1];
}
}
if(gup("urlc") != "")
{
window.location = gup("urlc").replace("lcamp", "&") + "#lcinside";
}
</script>
If you are just trying to scrub the referrer, maybe you should use meta refresh
<meta http-equiv="refresh" content="1;url=http://imgur.com/#lcinside">

javascript, if current url withour request part

I want do something: if something type url address like: www.mydomain.com/search(without request part), I want do something with javascript, fill with the url like www.mydomain.com/search?search=car, but nothing happened in my code.
<script>
var curent_url = document.URL;
if(document.URL.indexOf("?") < 0){ //if(!document.URL.match(/\?/)){
//alert('ok');
document.URL = curent_url+'?search=car';
}
</script>
Instead of document.URL, check for an empty document.location.search:
if (document.location.search.length === 0) {
// empty query string... append your part to doc
document.location.href = document.location.href + "?search=car";
}
Examine the document.location object in your console to see what else it offers. There's usually no need to parse out document.URL or document.location.href directly.
console.dir(document.location);

Categories