Jquery how to visit url + var? - javascript

The text in my input is: test
What I want to do is end up with the next result:
some url + var
http://google.com/test
function off(){
var visit = $('#visit').val();
window.location = (visit);
}
Something like that:
window.location = "http://google.com/(visit)";
But the var is not acceptable

function off(){
var visit = $('#visit').val();
var url = "https://google.com/"
window.location = url + visit;
}
Simple concatenation where url is what you want the URL to be and visit is whatever you're appending

Is that what you want?
window.location = "http://google.com/" + visit;

Related

Make every link or page with a dedicated reading page

I am trying to make every link or page with a dedicated reading page. For example, I have a lot of pages when visiting one page, storage is made, but the problem comes when he visits another page. He does not start again. He goes to the last id. I want to make each page with its own address.
<script>
window.onunload = function() {
var url_string = window.location.href ;
var url = new URL(url_string);
var value = document.getElementById("pageNumber").value;
localStorage.setItem("last", value , url_string );
var c = url.searchParams.get("c");
};
window.onload = function(){
var url_string = window.location.href ;
var url = new URL(url_string);
var c = url.searchParams.get("c");
var value = localStorage.getItem("last" , url_string);
if(value) {
document.getElementById("pageNumber").value = value;
window.location.href = "#" + value;
}
};
How do I do that and make each link have private storage
Another example https://hululkitaab.com/test/cct.html
https://hululkitaab.com/test/fcvcb.html
If you visited the first link and went to page number 20
When you visit the following link, it will return you to number 20 I do not want this any suggestions
what about this:
const pageName = "myPageName";
localStorage.setItem(pageName + "last", value);

How do I remove part of a string from a specific character?

I have the following url:
http://intranet-something/IT/Pages/help.aspx?kb=1
I want to remove the ?kb=1 and assign http://intranet-something/IT/Pages/help.aspx to a new variable.
So far I've tried the following:
var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if(link.includes('?kb=')){
var splitLink = link.split('?');
}
However this just removes the question mark.
The 1 at the end of the url can change.
How do I remove everything from and including the question mark?
Use the URL interface to manipulate URLs:
const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const url = new URL(link);
url.search = '';
console.log(url.toString());
var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if (link.includes('?kb=')) {
var splitLink = link.split('?');
}
var url = splitLink ? splitLink[0] : link;
console.log(url);
var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if(link.includes('?kb=')){
var splitLink = link.split('?');
console.log(splitLink[0]);
}
You can also try like this
const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const NewLink = link.split('?');
console.log(NewLink[0]);

bookmarklet to add a parameter to the url and resubmit it?

Is the following possible with a bookmarklet?
Add an additional parameter to the URL (include_docs=true)
Re-submit the URL
I have this but it fails silently on firefox. I haven't tried it with another browser:
javascript:(
function()
{
key = encodeURI('include_docs'); value = encodeURI('true');
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&');
}()
);
No need to over-complicate anything ;-)
document.location += '&include_docs=true';
That should do the trick. In bookmarklet form:
javascript:(function(){document.location+='&include_docs=true'}());

Get pathname along with PHP vars using JavaScript?

I want to save an entire URL paths to a variable, including the php vars, eg:
mysite.com/pagename?id=2
I can use
var pathname = window.location.pathname;
but this only retrieves the URL without the variables.
Is there a function to retrieve the URL as a literal string?
This should work
window.location.href
Have you tried see if it works:
document.URL
Can you try this,
// Get current page url using JavaScript
var currentPageUrl = "";
if (typeof this.href === "undefined") {
currentPageUrl = document.location.toString().toLowerCase();
}
else {
currentPageUrl = this.href.toString().toLowerCase();
}
Ref: http://www.codeproject.com/Tips/498368/Get-current-page-URL-using-JavaScript
It's hard , this answer explains how to implement it from the top response:
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
//var query = getQueryParams(document.location.search);
//alert(query.foo);

window Location - only protocol, hostname and pathname

Say I have this URL:
http://www.domain.com/subpage/?somsearch#somehash?
Is there a standard way to get the following result, without hash and search:
http://www.domain.com/subpage/
This is how I do it now:
var windowLocation = window.location;
alert(windowLocation.protocol + '//' + windowLocation.hostname + windowLocation.pathname);
Thanks to Bergi, this is pretty simple:
http://jsfiddle.net/8Vr7n/3/
var url = "http://google.com/?search=bob#asdf";
var shorterUrl = url.split("#")[0].split("?")[0];
alert(shorterUrl);

Categories

Resources