load() and getscript() not always working - javascript

On my index.php page I use the jquery load() to load other pages in a div "bodycontent". This works fine and dandy. I disable regular links, and must make the href the page name, without the php, and tag the php back on when the load() occurs. One of my pages I'm loading in the div I have dialog boxes pop up on links.
When you initially go to the page, this works fine, but if you steer away by clicking on "products" or any other link on the page at all, and then go back to "home" and click on a link "read more", the dialog boxes will not show up again. You have to refresh, and then it will work until you click a link again. Also, this works fine in my IE 9, ver9.0.8, but older IE at work (like 8), and at least one other persons chrome the dialog box will not come up at all. There are also IE9's at my work that this will just not work on, despite me deleting temp internet files, ect. On my computer it works fine on brand new downloads of firefox, chrome and IE, so I'm also wondering if my method is backwards compatible and cross browser, and if not what I can do to make it so. I'm also using get script in a function with the load to pull the jquery, jquery ui, and my javascript file for the dialog.
I've also looked at things like ajaxcomplete and such, but being mine initially works, I'm not really sure what to do.
My site main index page loading home.php initially (the news, ect)
Fiddle with my navigation javascript, which makes the pages inactive/active and load()/getscript in the div
dialog.js:
$(document).ready(function() {
$('#dialogbox').dialog({
autoOpen: false,
title: 'Loading title...',
modal: true,
width: 500,
height: 400
});
});
function readMore(id,title,cat,desc,post,auth) {
//alert(id +","+ title +","+ cat +","+ desc +","+ post +","+ auth);
var $dialog = $('#dialogbox').html('Category: '+ cat +'<br/>'+ desc +'<br/>---------------------------------<br/>Posted by: '+ auth +'<br/>' + post);
$dialog.dialog('open');
$dialog.dialog("option","title",title);
}
How I pull to function readMore:
<?php
require('config/dbconfig.php');
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);
/* fetch values */
while ($stmt->fetch()) {
//echo 'id: '. $id .' title: '. $title;
echo "<table border='0'>";
$shortDescLengthn = strlen($descn);
if ($shortDescLengthn > 106) {
$sDCutn = 106 - $shortDescLengthn;
$shortDescn = substr($descn, 0, $sDCutn);
} else {
$shortDescn = $descn;
}
echo "<h1>$titlen</h1>";
echo "<tr><td>$shortDescn...</td></tr>";
echo '<tr><td><a href="javascript:void(0);" onclick="'
. 'readMore(' . $idn . ',' . htmlspecialchars(json_encode($titlen)) . ','
. htmlspecialchars(json_encode($categoryn)) . ','
. htmlspecialchars(json_encode($descn)) . ',' . htmlspecialchars(json_encode($postdaten)) . ','
. htmlspecialchars(json_encode($authorn)) . ')">Read More</a></td></tr>';
echo "<tr><td>Written by: $authorn</td></tr>";
echo '<tr><td><img src="images/hardcore-games-newsbar-border.png" width="468px" /></td></tr>';
}
echo "</table><br />";
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
So again, basically I need to see why this isn't working after I click a link, and how it can be more cross browser and backwards browser compatible.

All I had to do was add:
$('#bodycontent').empty();
before the load(), and it works on every computer I've tried so far. I'm guessing the div needed to be emptied before loading new html in it, so going back it was keeping the js and such from getscript and messing up. Also only kept the single js file that was required only by home.php to be loaded, and put everything else in the index page (like it should have been anyways).

Related

Fire click event once per (different) class

I'm trying to have a click event fire on any tag with the same class only once.
I tried using '.one', although, it removes itself after the first class tag is clicked.
$(document).ready(function() {
$(".dropdown").one('click', function() {
...
});
});
The code is bought in from another .php file on the same directory with some server information so there are multiple (no end) in the number of tags with the same class.
<div class = \"dropdown\" id=" . $row["name"] . "dropdown" . ">
<div>". $row["email"] . "</div>
<div>name:" . $row["name"] ."</div>
<div>". $row["email"] ."</div></div>
";
Any recommendations?
As per your comments what you actually needs to do is apply a check of text available already or not?
$(document).ready(function() {
$(".dropdown").on('click', function() {
if(empty($('<text wrapper class or id>').text())){
// add code to bring the info
}
//rest other code.
});
});
Note : I fyou can show your code HTML then I can Update my code to give you concreete answer. The above will guide you how to proceed.

How to prevent those PHP variables from being cached?

I'm using this code in functions.php on WordPress to generate an affiliate link based on the visitor location, It's working perfectly but the problem is that if page caching is turned on (W3 Total Cache), The variables get cached so if someone from the UK was the first one to open the page then the second one from Germany opened the page he will get the same link that the first visitor got.
One more thing please, I'm still very new to PHP and javascript so I would appreciate if the answer was simplified enough.
<?php
add_action( 'woocommerce_before_add_to_cart_button', 'affiliate_link', 10);
function affiliate_link() {
$not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
// IP Geolocation
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
// Get Custom Fields
$de_asin = get_post_meta(get_post()->ID, "wccaf_de_asin", true );
$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );
//////////////////////////////////////////////
if ($country_code=="DE" or $country_code=="DE") {
$amazon_domain = 'https://www.amazon.de';
// $associate_id = 'bonstato-21';
$asin = $de_asin;
}
else if ($country_code=="GB" && $uk_asin!=="") {
$amazon_domain = 'https://www.amazon.co.uk';
// $associate_id = 'bonmedico-21';
$asin = $uk_asin;
}
///////////////////////////////////////////////
if( wp_is_mobile() ) {
// Amazon Link For Mobile
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';
}
</script>
<?php
}
else {
// Amazon Link For PC
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';
}
</script>
<?php
}
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="amzGo();"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
?>
A way to deactivate/disable W3 Total Cache on specific pages is to use another plugin called: "Plugin Organizer" by Jeff Sterup.
After installing and enabling it, go to settings (of the plugin) and follow the instructions to set it in the right way.
Once the plugin is enabled and settings are saved correctly, when you edit/create a new page using the editor you can see a checkbox that's show you which plugin to enable for that particular page and which to disable.
In this way you can disable W3 Total Cache on the page that's use your function on functions.php.
Hope it helps.
The most common way to bypass the cache is to serve this data via the WordPress json api and use java script to put the always fresh data into place.
The problem is not that the variable inside the code is being cached; what happens is that W3 Total Cache grabs the first page produced by your PHP code and store it in the hard disk of the server. Thenceforth, the browsers' requests are answered delivering the HTML static file stored in server's hard disk (PHP will not produce the same page again).
To solve this problem, you must transfer the "intelligence" inside your PHP code, which is responsible for create slightly different versions of your page, to your Javascript code. Bastian Haustein suggested one way to do that, using WordPress REST API. However, I would try first doing it in a simpler way: after page load, capture the link of your cached page and modify it according to the location of your visitor. Naturally, you will have to capture visitors geolocation also using JS - see the link suggested by cjmling: How to get visitor's location (i.e. country) using geolocation?

Open Random Post Using Javascript

I want my code when someone clicks the link then a link will open random of 3 posts between */android.html ,*/samsung.html and */xiaomi.html.
My code is like this
<?php
$YuuClass->update_dls($file['downloads'], $file_id);
echo '<script type="text/javascript">
var x=Math.floor((Math.random()*3)+ 1);
var xxx=null;
if(x=="1"){xxx="android.html"}
if(x=="2"){xxx="samsung.html"}
if(x=="3"){xxx="xiaomi.html"}
</script>';
redirect('https://safelink-ainodorama.blogspot.com/2017/02/"+xxx+"?url='.base64_encode($copy['webContentLink']));
exit();
?>
Where "+xxx+" will change random between */android.html ,*/samsung.html and */xiaomi.html when I click it.
I tried to click it with code like that only showing base64code.
Ex-link when I click
http://myweb.web.id/0aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL2Evc2RuMTgzcGVrYW5iYXJ1LnNjaC5pZC91Yz9pZD0xMUNoa0JpS3lvWkRMMGIxTWxYek4yeUt2NGRQcnJoMG8mZXhwb3J0PWRvd25sb2Fk
I want when I click it the URL will be like this
https://safelink-ainodorama.blogspot.com/2017/02/android.html?url=base64_code_here
where android.html can change randomly from 2 another page android, Samsung and Xiaomi.
Because javascript is client-side and php is server-side the javascript wont change xxx.
Instead have an array of your pages, then use array_rand to pick a random one.
Something like:
<?php
$YuuClass->update_dls($file['downloads'], $file_id);
$page = [
'android.html',
'samsung.html',
'xiaomi.html'
];
$url = 'https://safelink-ainodorama.blogspot.com/2017/02/'.$page[array_rand($page)].'?url='.base64_encode($copy['webContentLink']);
redirect($url);
exit;

Insert some text after a specific div into WordPress frontend

I am customizing the front end of Woocommerce, so I'd like to add the text "/ Per Month" after my final price before the user clicks the Add to Cart Button.
I have tried through javascript
jQuery('.woocommerce-variation-price').append(" / month")
but while it works when I run it from the console it fails when I run it through a file in the template. I can still the code when I check the page code but it does nothing.
I was thinking maybe I could achieve this through a WordPress hook.
I have inserted this in functions.php
add_filter( 'the_content', 'permonth_filter' );
function permonth_filter ( $content ) {
if ( is_single() ) {
$content .= '<div class="permonth-filter"> / Per Month </div>' . "
";
} // End IF Statement
return $content;
} // End wpcandy_filterhook_signoff()
That adds some text at the end of a blog post.
Is there a way to add some text after a specific div with a specific class? woocommerce-variation-price
Make sure that you are running your JavaScript after the page has loaded.
jQuery( document ).ready(function($) {
// Run your code
$('.woocommerce-variation-price').append(" / month");
}); // end document ready

PHP and Java Script interaction handling

I have a requirement where on clicking button, I need to show all the links captured. The java script code would be in a separate file test.php, and the links are captured in an additional file links_generation.php. The function 'getLinks()' has a conditional flow that if no of links is less than 5, it would display all the links, else it would display 5 links and additional text that "x more links are there".
Now, I have a requirement where on click of button, I would like to show all the links captured. I'm not sure how to capture this using javascript.
can someone pleaser help how to proceed with this.
function getLink($result,$currency,$lang) {
foreach($result as $id => $data) {
$num++;
if($num <= 5) $list = $list.linkName(substr($id,3));
}
$list = substr($list, 0, -2);
echo "<li>".$lang['pagelike1']." ".$lang['pagelike2'].": ";
echo $list;
if($num > 5) echo "...".$lang['and']." ".($num-5).$lang['other_ad'];
echo "</li>";
}}
PS: the code is slightly modified,and pls be assured that this is working. I would need to incorporate the new functionality. Would appreciate your help.

Categories