I am using html javascript to share a link on facebook.
I am using this code:
<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=test.com');" target="_blank">
Share on Facebook
Now i want to replace test.com with the current url link in the browser .
How to do it !
You can use window.location or window.location.href which provides the current URL. In your example, you can use something like the following:
<a href="javascript:" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location);" target="_blank">
For more information see Window.location on Mozilla.org which has the following description:
The Window.location read-only property returns a Location object with information about the current location of the document.
Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = 'http://www.example.com' is a synonym of location.href = 'http://www.example.com'.
I believe it should work if you use window.location.href, so your code would be the following:
<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);" target="_blank">
window.location.href will get the current url e.g.
window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);
Related
I want to create a custom hyperlink which passes current URL as one of the parameter to web app endpoint (abc.azurewebsites.net).
These are the versions I tried with thought process of getting the element by Id and update the href using basic HTML properties but no luck.
Approach 1
> <script> baseurl="abc.azurewebsites.net?="
function buildURL(item) {
> item.href=baseurl+window.location.href;
> return true; }
</script>
> </head> <body> <a onclick="return buildURL(this)" href="">Google</a> </body>
Approach 2
Click on this <a href=""
target="_self" id= "test" onclick = buildURL() >Link </a> to execute
<script>
var el_up = document.getElementById("test");
function buildURL() {
this.href = "abc.azurewebsites.net?=" + document.URL;
}
</script>
Any suggestions please. Is it even possible?
Update: Just to mention, I'm trying this from Azure Devops description section
I believe this will work:
<body>
<a id="link-to-edit" href=""> Click here to execute </a>
</body>
<script>
baseurl="https://abc.azurewebsites.net?="
link = document.getElementById("link-to-edit")
link.href = baseurl + window.location
</script>
This will use javascript to programmatically update the href to the baseurl plus current location as soon as the page/script loads. Then when you click the link will be executed properly. The key is that the href must be set at the time of clicking. In your examples, you set the href AFTER the user clicks and thus the link will not work as you intend. Hope that helps!
Currently working on a share button for Vk however having a dynamic site, I need the button to automatically use the correct page URL.
This is the code snippet they offer for a static site, how could I adjust this for multiple pages ?
<a href="http://vk.com/share.php?url=http://example.com" target="_blank">
Easiest way is by JavaScript:
document.getElementById("vkShare").href = 'http://vk.com/share.php?url='+document.location.href;
<a id="vkShare" target="_blank">Share to VK</a>
You can get the current page's URL by using a window object like this: window.location.href.
simple
works on all sites
function share(){
function copyToClipboard(text) {
window.prompt('share us',text);
}
copyToClipboard(window.location.href);
}
<button onclick="javascript:share();">share </button>
Using php to get the url, try to use $_SERVER['HTTP_HOST'] to get the host and $_SERVER['REQUEST_URI'] to get any query string attached to the url.
<?php
$uri = $_SERVER['REQUEST_URI'];
$host=$_SERVER['HTTP_HOST'];
//$complete_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//To use whether HTTP OR HTTPS
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === "on"){
$htp = "https";
}else{
$htp = "http";
}
$complete_url = $htp."//:".$host.$uri;
?>
<a href="<?=$complete_url?>" target="_blank">
Is there a way to encode a URL by default while using page.js
For example:
<a href="/some link">Click</a
This gives http://example.com/some link in the address bar instead of http://example.com/some%20link.
To achieve this you need to do
<a href={encodeURI('/some link')}>click</a>
Is there any other work around?
I think you will need to mention the url in this form:
Click
Which will mean
http://example.com/some_link
I am trying to get the following link to work.
<a href='#' onclick='window.open(" | &ESPP_Info_URL | ");return false;'>Employee Stock Purchase Plan Information</a>
Basically the &ESPP_Info_URL variable takes in a url so that the code below looks like...
<a onclick="window.open(https://...);return false;" href="#">Employee Stock Purchase Plan Information</a>
But when I click the url it just refreshes the page. Does anyone know how to get this to access the link within the window.open function?
Try this, You need wrap url with in single quotes ' - window.open('YOUR URL HERE')
<a onclick="window.open('YOUR URL HERE');return false;" href="#">Employee Stock Purchase Plan Information</a>
Here's an extended version with more dynamic functionality, jic:
http://jsfiddle.net/ofnh7gke/
And the smaller version:
http://jsfiddle.net/rqf3s72n/
Javascript
var sites = ['http://www.google.com','http://www.github.com'];
document.getElementById('btnStockPurchasePlan').onclick = function(){
window.open(sites[0],'_parent');
return false;
};
HTML
Employee Stock Purchase Plan Information
In my app, I have a field to fill by my user for their URL. After they fill I convert the url into a href link. But sometimes user does not add a http:// prefix to their link, so a link is not working properly..
Is there any way to open a link even without the http:// prefex in the href?
If anyone knows the way without using javascript is fine, else we can try with javascript.
Here is my
<a target="_blank" href="http://www.google.com">Google</a>
<br>
<a target="_blank" href="http://google.com">Google</a>
<br>
<a target="_blank" href="www.google.com">Google</a> // this is not work!
Live Demo
You should force the user to insert a valid url, for instance by using a proper inpout tag:
<input type="url" placeholder="Enter a valid URL - e.g. http://path.to/website">
In case you can't/you don't want to edit existing content
$('a[target="_blank"]').click(function(e){
e.preventDefault();
window.location = /^(http|https):/.test(this.href) ? this.href : 'http://' + this.href;
});