jQuery -> Change Window.href - javascript

How would I use jQuery to redirect the page to teh base page + some string?
Specifically, I need to set the window.href = current page without params + 'SomeString';

Try something like this:
window.location.href = window.location.pathname + 'SomeString';

var loc = window.location + '';
window.href = loc.replace('(\?(.+))','') + str;

Related

How to add parameter in Javascript and remove previous parameter

I had a code that works but the problem is it just append the parameter instead of replacing it with a new parameter, Here is my working code:
jQuery('#input_1_11').change(function(){
var type = jQuery('#input_1_11').val();
if(type=='Inspection'){
window.location.replace(window.location.href + "?Inspection");
}
else if(type=='Installation'){
window.location.replace(window.location.href + "?Installation");
}
else{
window.location.replace(window.location.href + "?Maintenance");
}
});
what currently happening is for example
www.test.com?Inspection
Then if I click again the input11 it goes to the link
www.test.com?Inspection?Maintenance
What I need is to replace each ?parameter every change by user.
use origin:
window.location.origin + "?Maintenance"
instead of
window.location.href + "?Maintenance"
You can do a thing - get the present url and split it into an array with ? as parameter for split. Get the first element of that array. It will be the url without parameters.
jQuery('#input_1_11').change(function(){
var type = jQuery('#input_1_11').val();
var present = window.location.href.split("?");
if(type=='Inspection'){
window.location.replace(present[0] + "?Inspection");
}
else if(type=='Installation'){
window.location.replace(present[0] + "?Installation");
}
else{
window.location.replace(present[0] + "?Maintenance");
}
});

Displaying Name from Browser link

is possible for example, to put this on browser
https://www.mywebsite.com/index.html + one X name, let's say Pedro
and once it opens it has a text
Automatically replace this with the name from domain link
So it would say,
Hi "Pedro", is possible? thanks!
You could do the following if your url looks like this.
https://www.example.com/Pedro
In order to ensure the DOM has loaded wrap it in a DOMContentLoaded event listener.
document.addEventListener("DOMContentLoaded", function(event) {
let name = window.alert(this.href.substr(this.href.lastIndexOf('/') + 1));
let new_h3 = document.createElement('h3');
new_h3.innerHTML = `Hi ${name}`;
document.getElementById("nameHolder").appendChild(new_h3);
});
or else you could do the following which is a bit more succinct.
let name = window.alert(this.href.substr(this.href.lastIndexOf('/') + 1));
document.getElementById("nameHolder")..innerHTML = "<h3>" + `Hi ${name}` + "</h3>";
Provided you can pass it as a query (i.e. https://www.mywebsite.com/index.html?Pedro), you should then be able to do something like this:
var name = decodeURIComponent(window.location.search.substring(1));
document.getElementById("nameHolder").innerHTML = "<h3>" + name + "</h3>";
Where you have some HTML element with an id of nameHolder.

Remove one parameter name and value in url using jquery

Existing Url
http://example.com/home.php?id=1&branch_id=4&course_id=5
New url
http://example.com/home.php?id=1&branch_id=4
removing course_id from existing url
How to remove one parameter value from url.
Tried below code:
function replaceUrlParam(url, paramName, paramValue){
var pattern = new RegExp('(\\?|\\&)('+paramName+'=).*?(&|$)')
var newUrl=url
if(url.search(pattern)>=0){
newUrl = url.replace(pattern,'$1$2' + paramValue + '$3');
}
else{
newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
return newUrl
}
but its not working...
As you said that you want to use variable instead of directly using .replace(/&course_id=\d+/, ''), you can do it like below:-
Example:-
var re = new RegExp("&course_id=\\d+");
var newUrl="http://example.com/home.php?id=1&branch_id=4&course_id=5";
console.log(newUrl.replace(re, ''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can simply do that by using this code to remove &course_id=5:
var url = "http://example.com/home.php?id=1&branch_id=4&course_id=5";
url.replace(/([&\?]course_id=5*$|course_id=5&|[?&]course_id=5(?=#))/, '');
It will return you: http://example.com/home.php?id=1&branch_id=4

Define image src tag using portion of an URL using JavaScript

With the code below, I am able to get a variable without a slash from the URL into my HTML.
var pathArray = location.pathname.split( '-' );
var URLwithoutDashes = pathArray[0];
var URLwithoutSlash = URLwithoutDashes.substring(1);
document.write(URLwithoutSlash);
Now I'd like to use it as my img src tag (with the extention .png), but I can't figure out how to do it. The code below doesn't seem to work.
var pathArray = location.pathname.split( '-' );
var URLwithoutDashes = pathArray[0];
var URLwithoutSlash = URLwithoutDashes.substring(1);
document.write("img src=\""URLwithoutSlash".png\">");
How can I solve this?
This line:
document.write("img src=\""URLwithoutSlash".png\">");
Should be:
document.write("<img src=\"" + URLwithoutSlash + ".png\">");
Use
document.write("<img src=\"" + URLwithoutSlash + ".png\" />");

Make word in into a link jQuery

I'm trying to make this:
<td class="Monthly Status-cell">/fileid=XXXX</td>
Display as
http://www.domain.com/fileid=XXXX
Can you tell me what is wrong with my code?
$('.Status-cell').replaceWith(function() {
var url = $.trim($(this).text());
return '' + url + '';
});
Thanks!
Use .html() instead of .replaceWith(). While using replaceWith you are replacing the td with anchor inside your table, which is invalid and that must be causing your alignment to get messed up.
$('.Status-cell').html(function(_, currentText) {
var url = "http://www.domain.com" + $.trim(currentText);
return '' + url + '';
});
Fiddle
Rather than replace the td with a link you should place the link in the td. Also you didn't add the domain to the link
$('.Status-cell').html(function() {
var url = window.location.protocol+'//'+window.location.host+$.trim($(this).text());
return '' + url + '';
});
http://jsfiddle.net/bUpYE/1/
Try
$('.Status-cell').html(function(idx, value){
value = $.trim(value);
return 'http://www.domain.com' + value + '';
})
Demo: Fiddle
Place the link in the cell using .html() instead of .replaceWith():
var domain = window.location.hostname; // current domain
$('.Status-cell').html(function(i, value) {
var link = domain + $.trim(value);
return '' + link + '';
});
http://jsfiddle.net/samliew/ZUYCX/

Categories