Using current url in a javascript function [duplicate] - javascript

This question already has answers here:
Get the current URL with JavaScript?
(28 answers)
Closed 8 years ago.
I have this js function
function twitterShare(){
window.open( 'http://twitter.com/intent/tweet?text='+$(".section-title h1").text() +'
'+window.location, "twitterWindow",
"height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0")
return false;
}
When the twitter window opens it grabs the .section-title h1 class, but I need it to say something like this,
"Check this out here: http://currentwebsiteurl.com"
I need a text string then grab the url of the page.
Any help would be greatly appreciated.

window.location.href contains the current URL of the page.
So your function should look like this:
function twitterShare(){
window.open('http://twitter.com/intent/tweet?text=Check this out here: ' + window.location.href,
"twitterWindow", "height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0")
return false;
}

You can use window.location.href. It holds the value of the current page you are on.

Related

How to get url parameter and add active class witt javascript/jquery? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
My links are like that:
index.php
index.php?do=that
index.php?do=this
And my current javascript code is like that:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
console.log(url);
$('.nav-link').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
So with this code even if I'm at index.php?do=this console log says to me /path/index.php
I think I need make changes on var url = window.location.pathname line but I couldn't.
How should I improve my code? There are bunch of similiar questions here in SO, but not the same one.
I appreciate any help.
In PHP you can get your URL parameters from GET array.
$_GET['do'] or try to print $_GET. This array will print all the URL parameters.
Hope this helps!

Check for url validation format [duplicate]

This question already has answers here:
How to check if a string "StartsWith" another string?
(18 answers)
Closed 7 years ago.
I would like to check the url that the user is saving and specially that it needs to be in a special format so i can use it at a later stage. The input field is to save a youtube video link and i would like to make sure that it is an actual youtube link or else present the user with an error stating it is not the proper format.
https://www.youtube.com/watch?v=y4Yp3-llWDs
I need to make sure that the beginning part of the url is proper always starting with this: https://www.youtube.com/watch
What would be the best way to go about this? regex?
var url = 'https://www.YouTube.com/watch'
if (/^https:\/\/www.youtube.com\/watch/i.test(url)) {
console.log('Valid YouTube URL Detected')
} else {
console.error('Invalid YouTube URL Detected')
}

Detect current page with JavaScript [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 7 years ago.
I'm wondering if Javascript can detect what page the user is on. Say for example that the user is on a "About" or "Contact" page, can JavaScript check if the pathway is "/About" or "/Contact" and then take action accordingly ?
How would that code look like?
I appreciate if anyone could give me a code sample
For example, you can get the URL by doing this:
window.location.href
If you want to know what age they are on, it really depends on the url structure. But you will get the information with window.location.pathname
var pathname = window.location.pathname;
switch(pathname) {
case "/home" :
console.log("home");
break;
case "/game" :
console.log("game");
break;
}

Need a Javascript URL Regex [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Wondering if someone could help with out with creating a regex..
Basically, taking an iFrame's src, and seeing if it's from SoundCloud. If it is, return its id. For example:
var src = 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F85110490&auto_play=false&show_playcount=false&show_user=false&show_comments=false&buying=false&liking=false&sharing=false&show_artwork=false&color=00e7ff';
function soundcloudId(src) {
var p = /___________/;
return (src.match(p)) ? RegExp.$1 : false;
}
soundcloudId(src);
And as a result, it would run the "src" through the regex, and if a soundcloud link, would return 85110490. Otherwise, false.
Try this regex:
/http:\/\/w.soundcloud\.com\/.*%2Ftracks%2F([0-9A-F]+)/
Runnable example: http://jsfiddle.net/mYf6P/

change url address using Javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to rewrite URL without refresh, like GitHub.com
let say I have variable in Javascript
var currentpage = 0;
my address page address is www.xyz.com/page/1
when I click somewhere and js function trigger
currentpage = 1
//Here I want to change/rewrite www.xyz.com/page/2
//Without redirecting to this page
See pushState (browser support currently limited).
You can't. You have to use the '#' or '#!' notation and pass the page number after it, then do trigger some js on load to figure out which page to display.

Categories