Js on click Open link, then copy text, then display an alert - php

I'm trying to develop a custom coupon system, and all works with IE, but when I try to use the following code on firefox or safari on click it open link and display alert but doesn't copy text:
<script type="text/javascript">
function copy_to_clipboard(text)
{
if(window.clipboardData)
{
window.clipboardData.setData('text',text);
}
else
{
}
alert('<?php echo get_option('custom_message'); ?> Powered by: WpCode.net Couponica');
return false;
}
</script>
And on the link:
<a onclick="copy_to_clipboard('code to copy')" href="link to open" style="margin-left:40px;" target="_blank">
What's wrong? How can I change this to make it work on firefox?

Clipboard access is not available in Firefox. Take a look at http://code.google.com/p/zeroclipboard/ for a Flash based alternative.

It is not possible to copy to clipboard in other browser then IE due security issues. You can use Flash for it, but from Flash 10.0 the security has increased aswell, so only users with Flash 9 or lower are able to copy.
What you can do is when you want someone to copy something, show a popup with a textbox with only the text the need to copy.

function copyLink(){
var link = window.location.href;
navigator.clipboard.writeText(link);
alert('link copy to clipboard');
}
<button class="btn btn-primary" onclick="copyLink()" >Share</button>

Related

Send query results to printer [duplicate]

Using php I created dynamical table, which contains all students from my database. I would like now to add an icon of printer, and when user clicks on that icon, to send this table to printer? I saw it on many pages, but how it's done? Tnx in advance...
Simply use window.print().
Example in jQuery:
<script>
$(function() {
$("#print").click(function() {
window.print();
});
});
</script>
<a id='print'>Print ME</a>
Example in JavaScript:
<script>
function printMe() {
window.print()
}
</script>
<input type="button" value="Print" onclick="printMe()">
Aside from window.print() it should be noted that JavaScript can't send the data to the printer directly - it can prompt the print dialog for you though. IE6 used to allow that, and visiting certain sites would make for surprises in your printer tray later that evening.
You can bypass the print dialog with a browser plugin.

Open URL in the New Tab : WordPress

When I uploads a image into the page using Add Media, I am setting url for that image, so that when user clicks on that image he should get redirect to another page. problem is, is get loading on same page.
I want it should get load in to another tab. I can't edit each href with target="_blank" as i have lots of images.
I just want to know is there any particular php file that should I edit or any other way is there.
Please help me to solve this problem
<script>
$(function(){ $("a img").attr("target","_blank"); });
</script>
Add this in <head>.
It's easy to set target="_blank" for images:
Click an image to activate its settings pop-up window.
Click the Advanced Settings tab.
Scroll down to "Target" and you'll see the "open in a new window" checkbox there.
For old images, you'll have to make the change manually.
Hope this helps!
You could use JavaScript window.open(), like:
function imgWin(win, winName, imgArray){
var dot, hsh;
for(var i=0,l=imgArray.length; i<l; i++){
(function(i){
dot = imgArray[i].lastIndexOf('.');
hsh = imgArray[i].slice(0, dot);
imgArray[i].onclick = function(){
open(win+'#'+hsh, winName, 'resizable=1');
}
})(i);
}
}
Then you could even get your images easily, like:
var imgs = document.getElementsByTagName('img');
imgWin('imgwin.php', 'My Images, imgs);
Note, that window is implicit, so you dont actually need to writewindow.open().open()` will work fine, in this case.

running a php code from a link without redirection

I want to execute a php code when a user click on a given link , i've tried the following code
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("up.php");
return false;
}
</script>
</head>
<body>
<center>
Click Me!
</center>
</body>
</html>
where up.php is a php code that implement android GCM mechanism similer to the one in the link : GCM with PHP (Google Cloud Messaging)
but unfortunately when the link is clicked nothing happens , the php code not executed ,and only the url of the page change form *****.com/test.html to *****.com/test.html# , and by the way I've tested the php code and its works fine so it is not a problem in php code , so what i have missed here? why the code is not executed?
i found the solution , the problem that the jquery library is not included probably in my code
you are using jquery. $.get() will be something like below.
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
for further help check http://api.jquery.com/jQuery.get/
Sometimes, you may need to call some JavaScript from within a link. Normally, when user click on a link, the browser loads a new page (or refreshes the same page).
This might not always be desirable. For example, you might only want to dynamically update a form field when the user clicks a link.
To prevent the load from refreshing, you could use the JavaScript void() function and pass a parameter of 0 (zero) as below.
<a href="JavaScript:void(0);" onclick="return doSomething(); " />Click Me!</a>
Now if you want to use href="#", make sure onclick always contains return false; at the end.
OR
Use href="javascript:void(0)"
You can also use jQuery Load
function doSomething() {
$('#result').load('up.php', function() {
alert('Load was performed.');
});
}
When you are using Jquery should be :
function doSomething(){
$.get('up.php', function(data) {
$('.data').html(data);
});
}
Link
To stop page refreshing, you can use the javascript void() function and pass parameter of 0.
Click Me!
<div class="data"></div>
This div is used to display up.php result.
Ok start of by using fire bug and watch the console and network panels
Ensure the browser is trying to get something and not returning a 404 or a php error on the get
Look into prevent default http://api.jquery.com/event.preventDefault/
To stop the original link functionality working
If you still want them to move to the link use a window location or...
Look into bootstrap link buttons
http://twitter.github.com/bootstrap/base-css.html#buttons
( the link button )

hyperlink should not be clickable if its already opened in new window

I have a hyper-link in my php page which opens in new window when it is clicked. But I want it should open only in one window at time, it should ne open if it is already open in new window of browser. Suppose If I click 5 times it is opening in 5 new windows, I want it only in one window. This problem is coming with Mozilla Firefox Please help me doing this with JavaScript or Jquery.
Thanks
thanks. and sorry to share incomplete information. Actually multiple links will be created through php code, which will be opened in different windows. I have used JavaScript to open them in different window. My problem is that same url should not open again if it is already open in window or if any url is clicked multiple times it should open only one window.
Here is my code :
PHP Code
<?php
$Class="class='links'";
$jsfun = 'javascript:openMyLink';
while(condition)
{
print "Click Here";
}
?>
JS code :
function openMyLink(arg1,arg2,arg3) {
var url = "http:///launcher.php?arg1="+arg1+"&arg2="+arg2+"&arg3="+arg3;
newwindow = window.open(url, "Mywindow",'height=596, width=795, left=0, top=0, resizable=yes, scrollbars=no, toolbar=no, menubar=no');
}
I tried to change the second parameter of window.open as dynamic also instead of "Mywindow" but it did not work too. Its not giving the issue with chrome or IE.
If you want to open a site in new window only once you clicked on it, use below code...
<a id="target" href="javascript:void(0)" onClick="myfunc()">click here</a>
<script type="text/javascript">
x=0;
function myfunc()
{
if(x==0)
{
x+=1;
window.open("<--- YOUR SITE URL --->");
}
}
</script>
On your first page have this code,
jQuery(document).ready(function($){
$("#theLink").click(function(e){
if($(this).attr("at")=='1')
{
e.preventDefault();
return false;
}
});
});
and your link should be having an attribute 'at' set to '0' just
like
<a id="theLink" href="demo.html" target="_blank" at="0">Click here to go away</a>
and let your children or newly opened page have this code.
window.opener.$("#theLink").attr("at","1");
$(window).unload(function() {
window.opener.$("#theLink").attr("at","0");
});
This is little tricky but works.
Worked great for me on IE8.

jquery weird behavior of page refresh when i replace content

I have a site with an flash player.
This codes servers me the flash player stored in player_codes.php:
<script language="javascript" type="text/javascript" src="songs_related/flashmp3player/swfobject.js" ></script>
<!-- Div that contains player. -->
<div id="player">
<h1>No flash player!</h1>
<p>It looks like you don't have flash player installed. <a href="http://www.macromedia.com/go/getflashplayer" >Click here</a> to go to Macromedia download page.</p>
</div>
<!-- Script that embeds player. -->
<script language="javascript" type="text/javascript">
var so = new SWFObject("songs_related/flashmp3player/flashmp3player.swf", "player", "290", "247", "9"); // Location of swf file. You can change player width and height here (using pixels or percents).
so.addParam("quality", "high");
so.addVariable("content_path","songs_related/uploads"); // Location of a folder with mp3 files (relative to php script).
so.addVariable("color_path","songs_related/flashmp3player/default.xml"); // Location of xml file with color settings.
so.addVariable("script_path","songs_related/flashmp3player/flashmp3player.php");
// Location of php script.
so.write("player");
</script>
This is how i include the player backend
<div id="main_music_player_for_the_site">
<?php include_once('player_codes.php'); ?>
</div>
If you go to this link here and press play on any song in the list the following happens:
I send a request to backend and set the song.
Confirmation comes to me.
I load the player from the player_codes.php (given above)
But when step 3 happens it refreshes! and gets stuck
It should not refersh its not refreshing in my localhost also.
Here is my JS that does the work (called after i get a response in step 2)
function callBackForLoadSong(data)
{
//alert(data);
if(data == "failed")
{
alert("Song requres you to login. Its easy to create an account! We will take you to the login page after you press ok");
window.location.replace(REDIRECT);
}
else
{
//alert("s");
$("#main_music_player_for_the_site").text("refreshing player...");
$("#main_music_player_for_the_site").load("player_codes.php");
}
}
Main thing thats bugging me is why is the page getting refreshed??? and getting stuck
You have the same HREF for every play buttons. Looks like your onClick event loads the songs?
You should have :
play
Instead of :
play
Right now, you tell javascript to load your song with the onClick event and you tell the browser to go to a different URL using HREF. javascript:void(0) will prevent the browser to execute the HREF.
Well the actual problem lied in my return value of false AFTER I EXECUTE MY CALL BACK!!!
Here is the corrected JS :
function callBackForLoadSong(data)
{
//alert(data);
if(data == "failed")
{
alert("Song requres you to login. Its easy to create an account! We will take you to the login page after you press ok");
window.location.replace(REDIRECT);
}
else
{
//alert("s");
$("#main_music_player_for_the_site").text("refreshing player...");
$("#main_music_player_for_the_site").load("player_codes.php");
}
return false;
}
so a return false; must be specified in the function also

Categories