My Url looks like: http://google.com/stackoverflow/post/1
Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:
Post . How to do it ? Thanks !
Plz try this:
var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];
Thanks.
UPDATED:
Using JavaScript:
var url = "http://google.com/stackoverflow/post/1"; // or var url = window.location;
var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);
if( matches ){
var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
alert( newURL );
}
document.getElementById('post_link').href = newURL;
HTML:
<a id="post_link">post</a>
See JSFiddle
use location Object
location.href
location.pathname
Related
My code below opens a random link and instead of opening the link in a new page I want it to open in an Iframe is this possible.
<button onclick="randomLink()" target="iframe_a";>Click here to go somewhere else!</button>
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length)
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
window.location = "http://" + link ;
}
</script>
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
Simply change the src attribute of the iframe, replacing the window.location = "http://" + link ; line to the following:
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
Or if you want to use jQuery:
$('iframe').attr('src', "https://" + link);
Just like in the #DinoMyte comment.
The javascript code would look like this:
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length);
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
};
The complete example is here: https://jsbin.com/cuzehu/1/edit?html,js,output
I was wondering is it possible to replace the %20 to a + whenever I click the button to add the textbox value to the url.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url;
});
});
Thanks
Yep with a regex for all occurrences $('#textbox').val().replace(/%20/g, '+');
I haven't tested it but this should work. You want to replace the spaces in the value. They're not encoded to the entity at this point.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
var textboxValue = $('#textbox').val();
textboxValue = textboxValue.replace(/ /g, "+");
url += textboxValue;
window.location = url;
});
});
Have you tried this?
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url.replace(new RegExp(' ','g'),'+');
});
});
When you're creating URL parameters dynamically, you shouldn't use ad hoc substitutions to encode them. Javascript provides the encodeURIComponent function to perform all required encodings. So you should write:
url += encodeURIComponent($('#textbox').val());
I'm trying to change the URL in the address bar using javascript.
So if the user access the page using
www.example.com/ajax/project8.html
Url should be changed automatically to
www.examp.com/#cbp=ajax/project8.html
shouldn't be any harder than this:
window.location = "http://whatever.you.want.com"
UPDATE
So you want your site to redirect to another page when the url is www.example.com/ajax/project.aspx?id=whatever and id=xxx could be any id.
To achieve that you need a function that returns the query string parameter value eg:id=whatever
Then check if the current url needs to be redirected to another page. If this is the case then redirect to new url with same parameter value.
/*
function that returns a query string parameter value
this function works with many parameters
Eg: www.example.com/#cbp=ajax/project.aspx?myParam=hello&id=1283&otherParam=234
to get the param value just give it the parameters name
getQueryStringValue("id") returns : 1283
getQueryStringValue("myParam") returns : "hello"
*/
function getQueryStringValue( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
//current url
var currentUrl = location.href;
//check if current url contains www.example.com/ajax/project.aspx
if (currentUrl.indexOf("www.example.com/ajax/project.aspx") != -1 ){
//new url for redirection
var newUrl = "www.example.com/#cbp=ajax/project.aspx?id=" + getQueryStringValue( "id" );
//redirect to new page
location.href = newUrl;
}
Try this code
if (window.location.href == 'www.example.com/ajax/project8.html') {
window.location = 'www.examp.com/#cbp=ajax/project8.html';
}
you can set all things like
window.location.href = "www.examp.com/#cbp=ajax/project8.html"
for more details how you will manage all url parameter then please see
JavaScript and jQuery url managment
window.location.href = "#cbp=ajax/project8.html";
you can change the value written after # to any location , div id etc.
e.g
window.location.href = "#myDivID";
<meta http-equiv="refresh" content="0; url=http://example.com/" />
Note: please put on header
or
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
I need to send a mail using javascript but i was unable to do it with
can any one please help with the code.
var link == "mailto:test#test.com"
+ "&subject=" + escape(":Feedback");
window.location.h ref = link;
You assign link variable with == which is not correct. Try with:
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;
Your javascript is not valid. You have a space in href and var link == should be var link =
This works.
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;
I'm getting confused passing the http:// in a string to the url as it is being stripped to
http%3A%2F%2F
I tried.. using the encodeURIComponent(http://)
but that didn't work either..
I'm trying to pass the url into here:
https://www.facebook.com/sharer/sharer.php?url=
Here is my code that isn't working:
$(document).on('click', '.fb', function(e) {
var findfb = $('.fb').parent().parent().parent().find(".zoomy").attr("src");
var facebook_url = 'http://www.facebook.com/sharer.php?url=http://www.site.com/folder1/'+encodeURIComponent(findfb)+
'&title='+encodeURIComponent('title of page');
window.open(facebook_url);
});
Just do it as simple as:
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb +'&title=' + 'title of page');
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb) + '&title=' + encodeURIComponent('title of page');