jquery script dosen't works in safari - javascript

I got problem with jquery code.It somehow doesn't work in safari browser. Rest of the browsers working fine but in safari I dont know why not any solution?
Index.php - I have this code inside HEAD
<script type="text/javascript">
$(document).ready(function () {
var prilohy = <? php echo json_encode($linkpar); ?> ;
if (prilohy == "Pizza") {
$('#content_donaska_extra').show();
} else if (prilohy == null) {
$('#content_donaska_extra').show();
} else {
$('#content_donaska_extra').hide();
}
});
</script>

Remove the space between <? and php
var prilohy = <?php echo json_encode($linkpar); ?> ;
script be
$(document).ready(function () {
var prilohy = <?php echo json_encode($linkpar); ?> ;
if (prilohy == "Pizza" || prilohy == null) {
$('#content_donaska_extra').show();
} else {
$('#content_donaska_extra').hide();
}
});

Related

How do I do an IF statement in my functions.php to determine if I am on the singles product page? (woocommerce)

Hi have made a php function that uploads script to pages with "cart" is_cart() and it all seems to be working properly. But when I try to use is_product() it doesn't register it on the single product page. How do I get my php to render script on the single product page?
I call this function in my functions.php
function refresh_update_qty() {
if (is_cart() || is_product()):
?>
<script type="text/javascript">
jQuery('div.woocommerce').on('click', ' button.plus, button.minus', function(e){
var math_method = '';
if(jQuery(this).hasClass('plus')) {
math_method = "1";
}else if(jQuery(this).hasClass('plus')) {
math_method = "-1";
} else {
// Do nothing
}
var this_input = this.parentNode.querySelector('input[type=number]');
var current_val = this_input.value;
var new_val = parseInt(current_val) + parseInt(math_method);
this_input.value = new_val;
document.getElementById('update_cart_button').disabled = false;
<?php
if(is_cart()):
?>
jQuery("[name='update_cart']").trigger("click");
<?php
endif;
?>
e.preventDefault();
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'refresh_update_qty' );
Based on woocommerce documentation the is_product() should work, but it doesn't. https://woocommerce.wp-a2z.org/oik_api/is_product/
I figured out the problem. It seems like my products page had the woocommerce class inside the body and my cart has it inside a div. so once I changed jQuery('div.woocommerce') to jQuery('.woocommerce') it worked.
Final code below
function refresh_update_qty() {
if (is_cart() || is_product()):
?>
<script type="text/javascript">
(function($) {
$('.woocommerce').on('click', ' button.plus, button.minus', function(e){
var math_method = '';
if($(this).hasClass('plus')) {
math_method = "1";
}else if($(this).hasClass('minus')) {
math_method = "-1";
} else {
// Do nothing
}
var this_input = this.parentNode.querySelector('input[type=number]');
var current_val = this_input.value;
var new_val = parseInt(current_val) + parseInt(math_method);
this_input.value = new_val;
<?php
if(is_cart()):
?>
// IF CART PAGE UPDATE CART
document.getElementById('update_cart_button').disabled = false;
$("[name='update_cart']").trigger("click");
<?php
endif;
?>
e.preventDefault();
});
})( jQuery );
</script>
<?php
endif;
}
add_action( 'wp_footer', 'refresh_update_qty' );

Disable right click not working

<head>
<script>
$("body").on("contextmenu", "img", function(e) {
return false;
});
</script>
</head>
<?php
session_start();
include 'connection.php';
include 'sessions.php';
$image_id = $_GET['id'];
$query = "SELECT * FROM medical_data WHERE md_id = '$image_id'";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$image = $row['md_pt_image'];
}
}
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'" height="auto" width="auto"/><br>';
?>
This is my code, I want display an image from database and disable right click on it.
But the JS is not working, i still can right click on the image.
Is it because I am using < img > in echo ? or my JS is wrong!?
As #haxxxton already said that you need to wrap your code in document.ready to make it work.
<script type="text/javascript">
$(document).ready(function(){
$('img').on('contextmenu', function(e) {
return false;
});
});
</script>
Another way of doing it with pure javascript is
<script type='text/javascript'>
function nocontext(e) {
var clickedTag = (e==null) ? event.srcElement.tagName : e.target.tagName;
if (clickedTag == "IMG") {
return false;
}
}
document.oncontextmenu = nocontext;
</script>
Neel had a perfectly good answer and in fact I upvoted it.
Consider this a second implementation: You can use preventDefault();
function nocontext(e) {
var clickedTag = (e==null) ? event.srcElement.tagName : e.target.tagName;
if (clickedTag == "IMG") {
e.preventDefault();
}
}
document.oncontextmenu = nocontext;
Just Use Like:
<div id="disabled"></div>
$(document).ready(function() {
$("#disabled").on("contextmenu",function(e){
return false;
});
});
Working JS FIDDLE LINK

Alert box not working with php

<?php if ($data == "") { ?>
<script type="text/javascript">
alert("review your answer1");
window.location.href = "index1.php";
</script>
<?php } else { ?>
<script type="text/javascript">
alert("review your answer2");
window.location.href = "index2.php";
</script>
<?php } ?>
Alert box is not showing in above code as well as page is also not redirecting.
What is wrong in it?
Do the other way around:
<script>
var data = <?php echo $data; ?>
if ( data === "" ) {
alert("review your answer1");
window.location.href = "index1.php";
} else {
alert("review your answer2");
window.location.href = "index2.php";
}
</script>
Improving your code, it may require some adaptation:
<script>
alert("review your answer<?echo $data;?>");
window.location.href = "index<?echo $data;?>.php";
</script>

Redirecting mobile-desktop versions manually

I have this code in my homepage
<?php
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
?>
and on the mobile version of my website, to get back to the destop version I inserted a link to this almost empty "redirect.php" page
<?php
$_SESSION['mobile']=0;
header("location: ../index.php");
exit;
?>
but I am not getting the desired behavior: once I enter the website with my mobile, I am correctly redirected to the mobile version, but once I click on the link to get the desktop version, I loop back into the mobile version. What am I missing?
Thanks! :)
Remember to call session_start() function before using sessions.
Better use this to reduce code:
<?php session_start();
if (isset($_GET["desktop"])) {
// DESKTOP
$_SESSION["mobile"] = 0;
...
} else {
// MOBILE
if (!isset($_SESSION["mobile"])) { $_SESSION["mobile"] = 1; }
if ($_SESSION["mobile"] == 1){ echo '<script>...</script>'; }
...
}
?>
And link to the homepage with ?desktop=1 to switch to the desktop version.
Based on your current code - You should have a separate session var that tell you that the user has manually reqeusted control.
<?php
if(!$_SESSION['manuall_overide'])
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
?>
<?php
$_SESSION['manuall_overide']=1;
header("location: ../index.php");
exit;
?>

jQuery if statement broken?

I have a php webpage where a user enters their details into a form and after submitting two sections, named profile and images, must then appear. Here is the javascript code:
jQuery(document).ready(function($) {
var registered = <?php echo $registered; ?>;
alert(registered);
if (registered == 'false') {
$("#profile").hide();
$("#images").hide();
}
if (registered == 'true') {
alert('hello');
$("#profile").show();
$("#images").show();
}
});
For some reason it does not go into the if statement at all, even though the alert shows registered's value as true. What could be the problem here?
var registered = <?php json_encode($registered) ?>
if(registered == true) {
alert(registered);
$("#profile").hide();
$("#images").hide();
} else {
alert('hello');
$("#profile").show();
$("#images").show();
}
You are mixing up the string 'true' with the boolean true. Try a version that uses booleans from start to finish instead:
var registered = <?= json_encode($registered) ?>;
if (registered) {
$("#profile").hide();
$("#images").hide();
} else {
$("#profile").show();
$("#images").show();
}
Edit - Thanks to Jon in another comment for suggesting using json_encode.
use JSON.parse() Takes a well-formed JSON string and returns the resulting JavaScript object.
//JSON.parse(registered);
var registered = <?php echo $registered; ?>;
if (!(JSON.parse(registered))) {
$("#profile").hide();
$("#images").hide();
}
else {
$("#profile").show();
$("#images").show();
}
Remove the quotes and try
jQuery(document).ready(function($) {
var registered = <?php echo $registered; ?>;
alert(registered);
if (registered == false) { //Remove the quotes
$("#profile").hide();
$("#images").hide();
}
if (registered == true) { //Remove the quotes
alert('hello');
$("#profile").show();
$("#images").show();
}
});
Reason in this demo.
var registered = true;
alert(registered == 'true'); //return false
alert(registered == 'false'); //return false
In JavaScript data type comparison is a bit inconsistent. So always try to compare same data-types.
so try:
var registered = true;
alert(registered == true); //return true
alert(registered == false); //return false
Change your code to:
if (registered) {
alert('hello');
$("#profile").show();
$("#images").show();
}
else {
$("#profile").hide();
$("#images").hide();
}
Try with this
jQuery(document).ready(function($) {
var registered = "<?php echo $registered; ?>";
alert(registered);
if (registered == false) {
$("#profile").hide();
$("#images").hide();
}
else if (registered == true) {
alert('hello');
$("#profile").show();
$("#images").show();
}
});

Categories

Resources