I want to provide a way to share the current page being viewed. All I want is a simple button that produces an alert (jQuery?) that says
Copy/Paste to share:
<current_url_here>
So the url of the current page can be copied and pasted wherever the user wants to paste it.
I think I'm close to having it but I'm new to jQuery and can't find how to get it working. This is what I have:
<button id="btn">Share</button>
<script src="https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script>
$('#btn').click(function() {
currLoc = $(location).attr('href');
document.querySelector('alert').textContent = currLoc;
});
</script>
I'm having trouble figuring out the right syntax to get the url printed to the alert window.
It would be more convenient if it were possible to have the button in a table cell that's always in the same place, always on the page, and gets the url from an iframe, a div, or a 'td' or 'th' in a table and prints it to the alert instead of having to place and position the button on every single page.
CodePudding user response:
jQuery is simply a helper library for javascript. You can use plain javascript to do this. In your example you don't have an element named 'alert'. If you're referring to a browser alert you can do:
$('#btn').click(function() {
alert(window.location.href);
});