Sim Posted June 26, 2020 Share Posted June 26, 2020 So I needed to turn every URL on my page into a jQuery/Ajax link (WHICH PARTIALLY WORKS). Every link works except for any new links(ones that get loaded from my Ajax call). So say I load ?page=crimes It will load perfectly in my php__cointainer. But now when the module HTML loads. Say the crimes, yo click the commit button URL ex: ?page=crimes&action=commit&crime=1 The Ajax no longer works. It loads a fresh page. var jq = $.noConflict(); jq(document).ready(function() { jq('a').on('click', function(e) { var url = jq(this).attr('href'); if(url.includes("?")) { e.preventDefault(); alert(url); jq("#php__container").load(url); } else { alert ("no url"); } }); update_stats(); }); Quote Link to comment Share on other sites More sharing options...
sniko Posted June 26, 2020 Share Posted June 26, 2020 (edited) 1. Make use of return statements so you don't need unnecessary if/else blocks 2. Look into Javascript linter (ie: eslint) so you have a consistent style and spacing in your code 3. Wtf is `vvvvvv`? That does not look correct. It looks like you want to check if the url has a query string. Use the native browser API for for this - `location.search` Edited June 26, 2020 by sniko 1 Quote Link to comment Share on other sites More sharing options...
Sim Posted June 26, 2020 Author Share Posted June 26, 2020 My code spacing is correct before I post it in here. #2 the vvvvvs was correct JS before posted here. It read URL.includes The unnecessary it's will be removed over done texting. BUT most importantly you mentioned everything but anything regarding the question at hand Quote Link to comment Share on other sites More sharing options...
sniko Posted June 26, 2020 Share Posted June 26, 2020 45 minutes ago, Sim said: BUT most importantly you mentioned everything but anything regarding the question at hand I did, I asked what `vvvvv` was about. Also for better help please start posting a minimal, reproducible example and adding more details to help debug the issue. 2 Quote Link to comment Share on other sites More sharing options...
AdamHull Posted June 26, 2020 Share Posted June 26, 2020 Does your inspector throw any errors? 1 Quote Link to comment Share on other sites More sharing options...
Sim Posted June 26, 2020 Author Share Posted June 26, 2020 There is no errors in the code what so ever. I click a link, it loads my NEW PAGE WITH LINKS using Ajax. The NEW LINKS that APPEAR on the NEW LOADED content don't load with Ajax. My guess is, the JavaScript code is not binded(terminology?) to the new content loaded? Edit: edited main post to fix vvvv things and removed commented code. Quote Link to comment Share on other sites More sharing options...
SRB Posted June 28, 2020 Share Posted June 28, 2020 (edited) var jq = $.noConflict(); jq(function() { jq(document).on('click', 'a', function(e) { e.preventDefault(); var url = jq(this).attr('href'); if (url.includes("?")) { jq("#php__container").load(url); } }); update_stats(); }); You need to attach to something higher than what you are re-writing. You can't re-write the dom and expect to still be attached to it. Edited June 28, 2020 by SRB Quote Link to comment Share on other sites More sharing options...
Sim Posted June 28, 2020 Author Share Posted June 28, 2020 Aight, I have gotten it to work to some extent. Once my content gets loaded into the php__cointaner div, I can get the link recognized with this new code. I watch it load the new content inside the div, then it redirects :(. So.. preventDefault isn't working properly. var jq = $.noConflict(); jq(document).ready(function() { jq('a').on('click', function(e) { var url = jq(this).attr('href'); if(url.includes("?")) { e.preventDefault(); jq("#php__container").load(url); } }); jq('#php__container').click(function(e){ if (jq(e.target).is('a')) { e.preventDefault; var url = e.target.href; jq("#php__container").load(url); //alert(url); } }); // update_stats(); }); Quote Link to comment Share on other sites More sharing options...
gamble Posted June 28, 2020 Share Posted June 28, 2020 Assuming the previous post is your exact code, you are missing the parenthesis after preventDefault. 1 Quote Link to comment Share on other sites More sharing options...
Sim Posted June 28, 2020 Author Share Posted June 28, 2020 24 minutes ago, gamble said: Assuming the previous post is your exact code, you are missing the parenthesis after preventDefault. Lol, thanks. I can't believe I overlooked that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.