jQuery load local iframe url with escaped characters for disqus commenting

I’m loading a custom page type that is just comments for a post. This is so I can use Disqus threads for easier usability when multiple loop posts are on a single page.

When loading an iFrame with the following structure I keep getting this syntax error. Are my escape characters wrong?

Read More
$(".boxyComments a").click(function (event) {
                    event.preventDefault();
                    var post_id = $(this).attr("rel");
    $(".commentsIframeBig")
    .get(0).contentWindow.location.href =
        $("<?php echo get_site_url(); ?>","/ajax-post-fold/",post_id);

What’s happening is the get retrieves the WordPress hook to print the site url (in this case it prints http://whateverdomainex.com for the 1st call, 2nd should print /ajax-post-fold/ and the last call should print the post ID so the entire url ends up printing as http://whateverdomanex.com/ajax-post-fold/2825.

Instead my Chrome console gives me the following message:

Uncaught Error: Syntax error, unrecognized expression: /ajax-post-fold/

Update

I’ve put this variable into place and called it rather than the $("<?php echo get_site_url(); ?>","/ajax-post-fold/",post_id); as the get reference:

var postLink = $("<?php echo get_site_url(); ?>"+"/ajax-post-fold/"+post_id);

Implemented as such:

$(".boxyComments a").click(function (event) {
                    event.preventDefault();
var postLink = $("<?php echo get_site_url(); ?>"+"/ajax-post-fold/"+post_id);
                    var post_id = $(this).attr("rel");
    $(".commentsIframeBig")
    .get(0).contentWindow.location.href = postLink;

Which gives me the following Chrome message:

Uncaught Error: Syntax error, unrecognized expression: http://www.theciv.com/ajax-post-fold/28448 

The URL that should be in the src attribute for the iFrame looks like it should be fine and good to go, so why is this syntax error still being output?

UPDATE

var postLink = "<?= site_url('/ajax-post/'); ?>"+post_id;

$(this).closest(".boxy").find(".commentsIframeBig")
.css("display","block")
.animate({height:  "100%"}, 500)
.get(0).contentWindow.location.href = postLink;

With the proper structure above, the custom page is now loading in the iFrame. However the additional construct of +page_id which includes the rel attribute containing the post’s id isn’t loading properly.

Moreover when calling the new url as it’s original custom page template, then adding the post’s id does not load the correct page with post id. Confused yet? Read it again. Took me awhile to write that sentence.

In any case, now my mission to have the post id load when adding the custom page and the post_id as an added string for the iFrame’s url to load properly.

update

Here is final working code to load Disqus comments into same page, pseudo multiple times.

Basically this is pushing a post id to the end of a custom page type, resulting in the post’s content and attributable elements being loaded into the custom page template.

When stripping that custom page template down to just show the comments for the page, you can create a load/unload reaction whereby you are only calling Disqus once, removing that instance and then loading it again when another Load Comments button is clicked within a subsequently loaded post on the same page. Yay. Multiple Disqus commenting on one page with minimal Ajax loading.

Here is the structure et al that is almost working for me. Only 2 bugs left. First is the secondary load when emptying, then reloading the new Disqus page into the Ajax element using the .ajaxComplete() callback function.

What’s happening now is the callback is basically not being fired at all. As far as I can tell. Clicking on it a second time however, does make the call. But this is due to the class parameters being met for the else statement.

Second bug left is I’m having a hard time figuring out how to get the appropriate elements to enlarge, while leaving the others the same size.

// Load comments and/or custom post content
    $(".boxyComments a").click(function (event) {
        event.preventDefault();
        $.ajaxSetup({cache:false});
        var post_id = $(this).attr("rel");      var excHeight = $(this).closest('.boxy').find('.initialPostLoad').height();
        var postHeight = $(this).closest('.boxy').find('.articleImageThumb').height();
        var postWidth = $(this).closest('.boxy').find('.articleImageThumb').width();

            // close other comments boxes that may already be open
            if($('.commentsOpen').length ) {
                console.log('comments are open');

                $('.bigBoxy').closest('.boxy')
                    .animate({height:(postHeight + excHeight)}, 500);

                $('.showComments')
                    .removeClass('bigBoxy')
                    .removeClass('commentsOpen');

                $('.commentsAjax')
                    .empty(function(){

                        $(this).closest(".boxy").find(".showComments")
                            .addClass("commentsOpen")
                            .addClass("bigBoxy");

                        $(".bigBoxy").find(".commentsAjax ")
                            .css("display","block")
                            .animate({height:  "500px"}, 500)
                            .load("http://<?php echo $_SERVER[HTTP_HOST]; ?>/ajax-post/",{id:post_id});

                        $(this).closest(".boxy")
                            .ajaxComplete(function() {
                                var excHeight = $(this).closest('.boxy').find('.initialPostLoad')
                                    .height();
                                var postHeight = $(this).closest('.boxy').find('.articleImageThumb')
                                    .height();
                                $(this).closest(".boxy").animate({height:  (postHeight + excHeight)}, 500)
                    });
                });

            } else {

                $(this).closest(".boxyComments").find(".showComments")
                    .addClass("commentsOpen")
                    .addClass("bigBoxy");

                $(this).closest(".boxy").find(".commentsAjax")
                    .css("display","block")
                    .animate({height:  "500px"}, 500)
                    .load("http://<?php echo $_SERVER[HTTP_HOST]; ?>/ajax-post/",{id:post_id});

                $(this).closest(".boxy")
                    .ajaxComplete(function() {
                        var excHeight = $(this).closest('.boxy').find('.initialPostLoad')
                            .height();
                        var postHeight = $(this).closest('.boxy').find('.articleImageThumb')
                            .height();
                        $(this).closest(".boxy").animate({height:  (postHeight + excHeight)}, 500)
                    });
            }

        });

Related posts

Leave a Reply

3 comments

  1. Okay, here’s full working code to do what you want. You’ll have to swap out a few placeholders for your actual code:

    <script>
      jQuery(document).ready(function($){$(".boxyComments a").click(function (event) {
    event.preventDefault();
        var post_id = $(this).attr("rel");
    var postLink = "<?= site_url('/path/'); ?>"+post_id;
        $("#myFrame").attr('src', postLink);
    });
    });
    </script>
    

    And sample divs & iFrame:

    <div class='boxyComments'>
    <a href='#' rel='some-url'>test link</a>
    </div>
    
    <div class=".commentsIframeBig">
    <iframe id='myFrame' height="500px" width="800px" src=''>     
        </iframe>
    </div>
    

    Tested it locally and it worked no problem. You might have been running into issues with it not properly accessing the iFrame. If you can give the iFrame an id that makes it easier.

  2. It’s because you’re declaring var postlink as a jQuery object. You just need to get it as a string that you can then pass to the iframe.

    var post_id = $(this).attr("rel");
    var postLink = "<?= site_url('/ajax-post-fold/'); ?>"+post_id;
    
  3. UPDATE 2

    Looks like the string shouldn’t be included within the <?= get_site_url() ?> after all.

    Instead I’ve created a few vars to affect it. Code updated below with answer:

    var postDir = "/ajax-post-fold/";
    var postLink = "<?= get_site_url(postDir); ?>"+"/ajax-post-fold/"+post_id;