3

Made some BBcode for phpBB to allow users to post flickr pics with certain tags. Each photo div in the thread needs to be unique, and the images load when the thread is loaded.

When it comes to the unique DIV, I'm stuck for a way to insert the uniquely named element into the DOM at the point the BBcode is inserted, THEN load the pics. And it appears I can't use PHP in BBcode, nor the templating tags - if I could easily make a unique photo element from the post ID and the flickr tag I'd be laughing. Oh, and I can't touch the template. It's all got to be within BBcode. So, here's how I make a unique ID:

 var flickrUser = "{URL}".split("/")[4];   
 var tag = "{URL}".split("/")[6];
 var photoDIV = flickrUser + "-" + "tag";

Or...there's an element called with a unique post ID just above I could possibly use:

<div id="p61789" class="post bg2">

I tried

var postnumber=$(this).closest('div[class^="post"]').attr('id');

but it always seemed to return the FIRST matching div on the page, not the NEAREST to the point of the BBcode. This element is two "divs" below

<div class = "content">

and below the user posting area there is:

<div id="sig61789" class="signature">

So where I'm completely stuck is navigating to prev() or closest() or parent() or indeed anywhere from the point where I am without having a $(this) link to reference.

So shouldn't something like:

$(this).prev('content').html('<ul class="thumbs" id=photoDIV></ul>');

or even

$(this).html('<ul class="thumbs" id=photoDIV></ul>');

work? Everytime I think I understand jquery it all goes hazy again...

EDIT: More detail added for Pointy:

<div id="p63167" class="post bg2 online">
  <div class="inner">
    <span class="corners-top">
      <span></span>
    </span>
    <div class="postbody">
      <ul class="profile-icons">
        <li class="edit-icon">
          <a href="posting.php" title="Edit post">
            <span>Edit post</span>
          </a>
        </li>
        <li class="delete-icon">
          <a href="posting.php" title="Delete post">
            <span>Delete post</span>
          </a>
        </li>
        <li class="report-icon">
          <a href="report.php" title="Report this post">
            <span>Report this post</span>
          </a>
        </li>
        <li class="info-icon">
          <a href="mcp.php" title="Information">
            <span>Information</span>
          </a>
        </li>
        <li class="quote-icon">
          <a href="posting.php" title="Reply with quote">
            <span>Reply with quote</span>
          </a>
        </li>
      </ul>
      <h3 class="first">
        <a href="#p63167">Re: Testing new bbcode - ignore</a>
      </h3>
      <p class="author">
      <a href="viewtopic.php">
        <img src="" alt="Post" title="Post" />
      </a>by 
      <strong>
        <a href="memberlist.php">xxx</a>
      </strong>» 13 Jun 2011 14:33</p>
      <div class="content">
        <script>var
        APIkey="xxx";head.js("/forum/jflickrfeed/jflickrfeed.min.js","http://jquery-lazy.googlecode.com/svn-history/r14/trunk/jquery.lazy.source.js",function(){var
        flickrUser="http://www.flickr.com/photos/xxx/tags/7thjanuary2010/".split("/")[4];var
        tag="http://www.flickr.com/photos/xxx/tags/7thjanuary2010/".split("/")[6];var
        photoDIV=flickrUser+"-"+"tag";$(this).html('
        <ul class="thumbs" id="photoDIV">
        </ul>');$.getJSON("http://www.flickr.com/services/rest/?jsoncallback=?",{method:"flickr.urls.lookupUser",url:"http://www.flickr.com/photos/xxx/tags/7thjanuary2010/",format:"json",api_key:APIkey},function(data){$('#cbox').jflickrfeed({limit:30,qstrings:{id:data.user.id,tags:tag},itemTemplate:'
        <li>'+'
        <a rel="colorbox" href="{{image}}" title="{{title}}">'+'
        <img src="{{image_m}}" alt="{{title}}" />'+'</a>'+'</li>'},function(data){$('#cbox
        a').colorbox();});});$.lazy([{src:'/forum/jflickrfeed/colorbox/colorbox/jquery.colorbox-min.js',name:'colorbox',dependencies:{css:['/forum/jflickrfeed/jflickrfeed.css','/forum/jflickrfeed/colorbox/example1/colorbox.css']}}]);});</script>
        <ul id="cbox" class="thumbs"></ul>
      </div>
      <div id="sig63167" class="signature"></div>
    </div>
  </div>
</div>
digitaltoast
  • 659
  • 7
  • 23
  • If you'd post a representative snipped of what the entire relevant fragment of the page looks like, somebody can probably help. It's hard to tell exactly what the page structure looks like from what you've written above. – Pointy Jun 13 '11 at 16:16
  • Also, when you're trying to select something by class, you can just use "div.post"; you don't have to use an attribute selector. – Pointy Jun 13 '11 at 16:17
  • Thanks, @pointy. I've added more detail. – digitaltoast Jun 13 '11 at 17:09
  • Well it looks like a basic issue is going to be that scripts don't have much of an idea of where their ` – Pointy Jun 13 '11 at 17:32
  • @pointy not sure I understand what you mean by _when you're trying to select something by class, you can just use "div.post"_ - post is to "Load data from the server using a HTTP POST request." As for the scripts not knowing where their script tag is - not sure I follow that either. The scripts are inserted within script tags by the bbcode as shown in the added code above, everything works absolutely perfecly with the exception of generating a unique ul id at the point the code is inserted, or above or below the nearest unique div from that point. – digitaltoast Jun 13 '11 at 17:39
  • 1
    No no - your `
    ` element with class "post" that you were trying to find with that "[class^=post]" selector is what I was talking about. You can do that with just ".post".
    – Pointy Jun 13 '11 at 17:40
  • 1
    And the thing is, like I said, the code doesn't know anything about a "point" in the document; it has no concept of a "position" in the DOM. – Pointy Jun 13 '11 at 17:41

2 Answers2

1

the problem is you're assuming that the this context when you're executing within the script block is the script block itself, which is incorrect. If there is no context explicitly given, then your context is the window DOM element. Is there any reason you can't move this code to the page level, and initialize all the posts at page load?

$(function() {
    $('.post').each(function(i,item) {
        console.log(this); //Post DOM element. 
        //execute your flick retrieval code here.
    });
}
Tyler Schroeder
  • 730
  • 3
  • 6
  • 14
  • Ahhhhhhhhh! Lightbulb of realisation of waste of last 12 hours, and thanks for pointing that out. So this doesn't mean "this" and I also learnt that "live" doesn't mean "live". I wonder if anyone's collated a list of jquery functions which don't mean what they say?! I'll try your function - although when you say "initialize all the posts at page load" only one post in 20 or so has flickr images; I'll sleep on it and try and work it through tomorrow. Thanks! – digitaltoast Jun 13 '11 at 19:53
  • @digitaltoast: How about `add` (which I would name plus) or `after` (`a.after(b)` actually puts b after a) or `closest` which is the closest _parent_ and not the closest _element_? Actually they all seem natural to me but if you take the names too literally they could mean something completely different - the peril of short names, I guess. – configurator Jun 13 '11 at 20:21
  • @pointy - thanks for your suggestions. With your help and a few headaches along the way, I know have a way for users to post pictures in our forum. Code: http://pastebin.com/ZTyBreaR bbcodes for phpbb: http://www.phpbb.com/community/viewtopic.php?p=13009040#p13009040 demo: http://www.reading-forum.co.uk/forum/viewtopic.php?f=20&t=6536&p=63321#p63321 . Big thanks to all concerned. – digitaltoast Jun 15 '11 at 10:28
0

Have you tried Javascript's DOM functions to add it? http://www.javascriptkit.com/javatutors/dom2.shtml

For example, to insert right after the current script, with id "script1":

document.getElementById("script1").parentNode.appendChild( newelement );

You can add raw html too, just set innerHTML of the new element.

NoBugs
  • 9,310
  • 13
  • 80
  • 146
  • But if I was able to apply the unique identifying ID to the script itself, then I'd not have the problem generating the ID for the ul element. Unless I've mis-understood, doesn't that leave me in a chicken and egg situation? – digitaltoast Jun 13 '11 at 19:51
  • Can you start the script with – NoBugs Jun 13 '11 at 20:36