Get current page URL in href - javascript

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">

Related

How to give the current page url to HTML element property?

I have a HTML element
<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="CURRENT URL">Friend</a>
How can I set the value of data-link to the current url.
Do you want it in javascript? Since you didn't put PHP in your tags, I would assume so.
In that case, you can use window.location.href to do it in javascript.
document.getElementById("share-button-1").setAttribute("data-link", window.location.href);
or in jquery:
$('#share-button-1').attr("data-link", window.location.href);
in case you did mean through PHP, you could use the following:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]. In this case I'd use it like this:
<?php
$dataUrl = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
?>
<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="$dataUrl">Friend</a>
You can do like this using location interface
var _getCurUrl = location.href;
$("#share-button-1").attr('data-link',_getCurUrl);
JSFIDDLE

How to open an https website on a button click javascript?

The piece of code I am using is
window.location.href = "https://www.google.com/";
But i stays on the same page,doesn't go to google.com.I was able to get this work with an http page.
window.location.href = "http://www.tutorialspoint.com/mongodb/mongodb_insert_document.htm";
This is the complete code for the same
<form name="form1" onsubmit="submis()" action="/post.php" method="POST"
and the function submis()
function submis()
{
window.location.href = "https://www.google.com/";
}
and post.php
if (!empty($_POST))
{
//saving the contents of the post request to a file.
}
<script>
function goto(){
window.location.href = "https://www.google.com/";
}
</script>
in html code go to google
it works
I was able to make this work by changing the code to,
window.top.location.href="https://www.google.com"

Facebook share with javascript

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);

Make Page available only thorough redirect and not by direct url

I have some webpages which I want to appear only through internal redirects, such that even if someone just types in the url they wouldn't go through. To see them you would need to go through the page that redirects to the 'secure page'.
You can use HTTP_REFERER from the $_SERVER super global.
At the top of the restricted page:
<?php
// if the referrer is not the page you want the traffic coming from (http://yourdomain.com/allowed_from_page.php), redirect back to that page.
if($_SERVER['HTTP_REFERER'] !== 'http://yourdomain.com/allowed_from_page.php')
header('Location: http://yourdomain.com/allowed_from_page.php');
Add this on the page that will redirect to the specified page:
<form action="./redirectedpage.php" method="POST">
<input type="hidden" name="protection" value="yes"/>
<input type="submit" value="Go to Protected Page">
</form>
Add this on the page that can not be acessed directly:
<?php
$unlock = $_POST['protection'];
if ($unlock == 'yes'){
?>
**:: YOUR CODE GOES HERE ::**
<?php
} // END OF THE LOCK CODE
else {
// REDIRECTS USER TO HOME IF HE IS NOT UNLOCKED or IF HE ACCESS THE PAGE DIRECTLY
header('Location: http://myhomepage.com/index.php');
}
?>
Keep in mind that this is a simple example, that do not uses sessions and similars. If you need something more "SECURE" you will need to use, SCAPE($unlock = mysql_real_escape_string($_POST['protection']);), and other security stuffs. If you didn't understood please let me know it.

Replacing GET with POST

Here I am trying to redirect new page and I want to send a variable. It uses GET.
How can I do it with POST?
window.location.href = 'start.php?userid='+userid;
start.php
<?php
$user_id=$_GET['userid']; //should be post
?>
You will have to submit the data to the server, not just redirect the browser.
In your case, as it looks like you want full page refresh anyway just create a form on the fly:
var oForm = document.createElement("form");
oForm.method = "POST";
oForm.action = "start.php";
var oInput = document.createElement("input");
oInput.name = "userid";
oInput.value = userid;
oForm.appendChild(oInput);
document.body.appendChild(oForm);
oForm.submit();
You need to declare a fake form in HTML and a link that will trigger javascript to submit the form, like below
<form action="start.php" method="post">
<input type="hidden" name="userid" value="[userid]">
</form>
<script type="text/javascript">
function submitlink()
{
document.forms[0].submit();
}
</script>
<a class="buttondown" onclick="submitlink()">Submit Link</a>
I think this answer may help you:
How do you force a web browser to use POST when getting a url?
You just need to create a form on demand using javascript to send data with POST method when clicking a link.
So basically it's just this part:
<script type="text/javascript">
function submitAsPost(url) {
var postForm = document.createElement('form');
postForm.action = url;
postForm.method = 'post';
var bodyTag = document.getElementsByTagName('body')[0];
bodyTag.appendChild(postForm);
postForm.submit();
}
</script>
this is my post link
I was in the same problem you had. If you are using jquery.redirect.min.js as your jquery plugin, You can use as below. It gives you POST method.
$().redirect("myPhp.php", { name: "John" });
All you need is to download jquery.redirect.min.js file from here and link it with your php file, and use as above. That's it. Hope I helped.
Works fine for me.
You can post form by html tag and . Otherwise, see "jQuery.post" for async post.
For the 1st case you create FORM tag, put INPUT type="hidden" inside and set its value that will be posted.

Categories