Open link in a new window with javascript - javascript

I want to open links in a new pop up, I have my link in a PHP Code but when I try to add a on-click I keep getting errors.
This is my code:
echo "<td>Edit</td>";
Where exactly do I need to add the on-click function? Because it's easy in html but I keep getting errors because it's in PHP
Thanks

Add target="_blank"
_blank : Opens the linked document in a new window or tab
echo "<td><a href=\"edit.php?id=$row[id]\" target='_blank'>Edit</a></td>";
For a popup solution add this (attention to the various " ' )
target="popup" onclick="window.open('yourlink','name','width=xxx,height=xxx')"

echo "<td><a href=\"edit.php?id=$row[id]\" target='_blank'>Edit</a></td>"
another way is write a js function open_window = function(){your stuff} and
echo "<td><a href=\"edit.php?id=$row[id]\" onclick='open_window()'>Edit</a></td>"

You need to use window.open javascript functoin. Here is an example:
<a href="#" onclick='javascript:window.open(url, "", "width=200, height=100");' >Edit</a>
You may want to specify the javascript code in a separate function as well.
See http://www.w3schools.com/jsref/met_win_open.asp for more information on window.open function.

Related

CSP convert inline onclick event with passing a PHP variable

I generate a list of applications from MYSQL using php
and then when you click on the relevant line it will open the application in a window,
something like:
<?php
$Path = $ApplicationPath
print "<td class='centerTextIncPointer'><img src='../images/printicon.png'onclick='printTheWindow($Path)'/></td>\n";
?>
The above is blocked by CSP policy, how to I get the popup window with the application to work with CSP?
I tried to use the following example but could not figure out how to pass the path as my Javascript skills are not great and the list is generated dynamically.
<?php
$Path = $ApplicationPath
print "<td class='centerTextIncPointer'><img id='LineId' src='../images/printicon.png'/></td>\n";
?>
script.js:
document.getElementById("LineId").addEventListener("click", myFunction);
function myFunction(){
// open the app in the window
}
Assistance is greatly appreciated :)
You can try this code:
<img src='../images/printicon.png' data-path="<?php echo $path; ?>" onclick="return !window.open(this.getAttribute('data-path'), 'Google', 'width=500,height=500')"/>

PHP a href echo with span not functioning properly

I have the code
echo '<span class="imperial-username">';
echo'<a target="_blank" href="profile/'.$row['name'].'">'.$row['name'];'</a>';
echo'</span>';
which functions fine when i press the name it takes me to the profile. But if I click the image also above the name it will go to the person who is above on the list, as I have a list of links. And only goes to there profile if you click the name specifically.
Also all other links on the page seem to be not functioning properly and going to random profiles when pressed, the links are carrying over down the page or something is what I assume, maybe code not closed properly, but I see nothing that should be causing this.
You have a semicolon ; instead of a dot . in .$row['name'];'</a>';.
It is not a syntax error, because the instruction '</a>'; is valid but has just no effect.
please try this
echo '<span class="imperial-username">';
echo'<a target="_blank" href="profile/'.$row["name"]>'.$row["name"].'</a>';
echo'</span>';
echo '<span class="imperial-username">';
echo '<a target="_blank" href="profile/'.$row['name'].'">'.$row['name'].'</a>';
echo '</span>';

JS Not firing on click

I am trying to load the function call_ajax_add_to_quotelist via the button with the following code:
$cartlink .= '<a class="add_to_cart button alt" href="javascript:void(0);" onclick="call_ajax_add_to_quotelist(add_to_quotelist_ajax_url,'.$product->id.');" '.$style.'>'.$label.'</a>';
The code above is loading fine on the view source however when clicked it is showing dead with no console error I have loaded the js file in the function (It belongs to another plugin I am hacking a WP plugin with the same actions of another plugin)
Script Load:
$quotePluginJSUrl = site_url().'/wp-content/plugins/dvin-wcql/js/dvin_wcql.js';
?>
<script src="<?php echo $quotePluginJSUrl; ?>"></script>
<?php
I would a t first check, if call_ajax_add_to_quotelist is really a function in JavaScript Console, and if add_to_quotelist_ajax_url is a correct value.
Also, it is recommended to not use onclick. I recommend using jQuery event binder .on().
Expl.:
<?php
$cartlink .= "<a class='add_to_cart button' data-id='{$product->id}'
href='javascript:;' {$style}>{$label}</a>";
// ... more products
?>
// **one** <script> after all products
<script>
jQuery(window).on('click', '.add_to_cart.button', function() {
call_ajax_add_to_quotelist(add_to_quotelist_ajax_url, $(this).data('id');
}
</script>
Close the anchor
<a>...</a>
because you are adding the anchor dynamically, you need to use .addEventListener if you're using JS or Event Delegation if you're using jQuery
e.g.
$('.button').on('click',function(){
call_ajax_add_to_quotelist(add_to_quotelist_ajax_url,'.$product->id.');
});
Plus you haven't closed the anchor tag in your code which might cause you some problems:
$cartlink .= '<a class="add_to_cart button alt" href="javascript:void(0);" '.$style.'>'.$label.'</a>'

sending value with link with javascript

I am viewing the content of mysql inbox_messages table and view every message as link to reply the message or delete . so i have to send the message id with the link to specify the message in the editing page the code was
<a href="sompage.php?m_id=<? echo $m_id; ?>" >
and it's working.
But when I tried to make it with javascript to make the reply like chat box i dont know how to send the message id while opening and to get the id with php and i am opening the message in a div with this code
<a href = "javascript:void(0)"
class="display"
onclick = "document.getElementById('responsecontainer').style.display='block';document.getElementById('fade').style.display='block'"
style="font-weight:normal;">
<a href="sompage.php?m_id=<? echo $m_id; ?>" >
can be replaced by appending ?m_id= and a variable equal to whatever $m_id is.
Example:
var m_id = <?php $M_id ?>;
var yourLink = document.getElementById("replyLink");
yourLink.href = yourLink.href+"?m_id="+m_id;
That said, getting $m_id into the javascript without inline PHP in a synchronous manner would probably require the use of GET parameters, which is another topic, so I'll just give you a helpful link to get started:
http://javascriptproductivity.blogspot.com/2013/02/get-url-variables-with-javascript.html

Why should I click double on an 'a href' attribute to reload the page?

I have such a problem:
When I click on the element the url changes but PHP code doesn't run, but when I click it again or just push 'Enter' in the url, or push F5 the page reloads and PHP code runs
<a href="?action=out" title="Forget me!">
<img src="images/icons/exit.png" id="exit" class="ava">
</a>
Here was my html code.
Here is PHP one:
if($_GET['action']=='out')
{
//do smth. here
}
What is the problem, I just can't get it..
maybe I just have to use JS (location.reload() and so on), but I think that it will be not a good descision...
Thank you in advance!
I believe your href is wrong you have information in the href but no page assigned, if you are sourcing your own page try
<a href="<?php echo $_SERVER['PHP_SELF'].'?action=out'; ?>" title="Forget Me!">

Categories

Resources