Open ColorBox with link generated by hashchange event - share

Our company wants to include a LinkedIn Share Button in the news section of our website. It is relatively simple and consists of a carousel that open up the news items individually in Colorbox windows. We want the LinkedIn button to be within the Colorbox windows so that we can share the details of each news item.
So, I have successfully got the hashchange event to work when Colorbox is activated in order to show the correct url for each news item and the LinkedIn button does return the correct url when the news item is shared, however Colorbox doesn't open, it simply links to the index page of our site. My question is how do I fire up Colorbox from this shared link?
I have researched a lot of similar questions but cannot seem to get it working. Any help would be much appreciated. Thank you.
Below is my js and also a jsfiddle: http://jsfiddle.net/stegern/WvfsA/11/
$(document).ready(function()
{
//Carousel for news items
$('#news-carousel').show();
$('#news-carousel').jcarousel({
vertical:true,
scroll:true,
wrap:'circular'
}
);
$('.popup').colorbox({
title: function()
{
var url = $(this).attr('href');
return '#' + url;
},
onOpen:function()
{
window.location.hash = $(this).attr('href');
},
onClosed:function()
{
window.location.hash = "";
},
opacity: 0.7,
transition: 'fade'
}
);
//Attempt to open ColorBox when url is shared
$(function(){
var hash = window.location.hash;
if ('onhashchange' in window)
{
window.onhashchange = hashChanged;
}
else
{
setInterval(function(){
if (window.location.hash != hash)
{
hash = window.location.hash;
hashChanged();
}
}, 500);
}
var hashChanged = function(){
$.colorbox();
}
}
);
});
UPDATE
I have done some more research and discovered that I need to load my content in an iframe rather than using Ajax. I then need to add a querystring to my news item links and parse the parameters from the querystring in order to pass them to ColorBox.
However I am now getting a semantic issue with my js (line 8 Expected ')' token) which I don't know how to resolve. Can someone please explain.
Here is my html markup:
<ul>
<li>News Item One
</li>
<li>News Item Two
</li>
<li>News Item Three
</li>
And here is my js:
function getParameters() {
var
settingsObject = {},
hash,
hashes = location.search.substring(1).split(/&/),
i;
for (i = 0; i & lt; hashes.length; i++) {
hash = hashes[i].split('=');
settingsObject[hash[0]] = hash[1];
}
return settingsObject;
}
$('a.cb').colorbox($.extend({
iframe: true,
width: '800',
height: '600'
}, getParameters()));
I also have a jsfiddle setup at: http://jsfiddle.net/stegern/NtSvg/7/

Try putting some example code in a fiddle at http://jsfiddle.net/ then share here.
You posted your js, but we don't have the markup you're trying to use it on, so post the minimum necessary html code to make your example work in a fiddle.
It will help others visualize your problem much easier and quite possibly get you a solution a lot faster.

Ajax isn't loading because browsers typically disallow cross-origin file access for security reasons.Since the main code is hosted on jsfiddle, it forbids you to load pages from your site via ajax.
A quick workaround, if you're using Chrome, you can start it in a less secure mode, like indicated here: https://superuser.com/questions/593726/is-it-possible-to-run-chrome-with-and-without-web-security-at-the-same-time
I just tested now by opening a command prompt in the folder where chrome.exe is located and ran chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
Then I opened http://jsfiddle.net/WvfsA/12/ , where I stripped down your js to the minimum. You'll see your content is now loaded via ajax by colorbox, however, you're doing something wrong with those paths, because the images can't be found.
I took a look at http://jsfiddle.net/WvfsA/13/ and I'm not sure exactly why you have 2 nested $(function () {}), I saw that in Framework & Extensions, ondomready is already selected, so you don't really need to wrap your main function(s) in anything.
Here's a quick screenshot as proof that it works:
http://i.imgur.com/jAiUW28.png?1
When you were developing, were you running your example through a server? You need to have a local server in order for anything ajax-related to work.
Install XAMPP http://www.apachefriends.org/en/xampp.html if you haven't already?
Edit: or you could develop on Chrome launched with that flag I mentioned, to bypass the need of a local webserver, but it's not a really good idea.

Related

FF Addon SDK page mod script to content script communication not working

For starters, I've been trying to allow communication from a page script to a content script. If the docs are accurate, this should be easy. Here's what I'm doing, I believe fully in accordance with https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/content-scripts/communicating-with-other-scripts.html#Using%20the%20DOM%20postMessage%20API :
And here's my live test case:
main.js:
exports.main = function() {
var data = require('sdk/self').data,
pageMod = require('sdk/page-mod');
pageMod.PageMod({
include: '*',
contentScriptFile: data.url('postMessageRelay.js'),
});
};
postMessageRelay.js
// Trying with window.addEventListener also doesn't work
document.defaultView.addEventListener('message', function (e) { // e.data, e.origin
console.log('would be good if it got here:'+e.data);
});
console.log('it gets here at least');
And the JavaScript within a regular HTML file (on a normal remote server, not file or localhost):
try {
window.postMessage('webappfind', window.location.href);
}
catch(e) {
alert(e);
}
This really looks like either a full-blown bug for this functionality or a problem with the docs... I had similar problems trying to communicate via custom events so going a little bananas...
Answered in Bug 910972, but leaving it here to for future visitors of SO:
The issue was with the page immediately firing postMessage in the head tag, so the page-mod script isn't even yet attached to the page to listen to the message event. The communication back and forth between page and content scripts as in this example works as long as this timing is taken into consideration

jquery.form.js is not working in Internet Explorer 9

I have a simple form with a file upload control. The form will be posted once I click on the upload button. I'm trying to do a jquery-ajax post, using the jquery.form.js.
My code is as follows,
var options = {
beforeSubmit: function (formData, jqForm, options) {
$(".loaderImage").show();
return true;
},
success: function (responseText, statusText, xhr, $form) {
$("#result").html(responseText);
},
error: function(xhr) { alert(xhr.responseText); }
};
$("#AjaxFileUpload").ajaxSubmit(options);
return false;
It works fine in Google Chrome, Firefox and Internet Explorer 10. The problem with Internet Explorer 9 is, after debugging, it doesn't enter the success(). Any pointers on what's going wrong? There are no errors in the console either.
I added the error option as well, but the issue is the same, the breakpoint doesn't hit the alert.
I just had a look at the network traffic. There is no POST request going (in Internet Explorer 9) when I click the upload button, but there's a POST request going in Internet Explorer 10.
I cleared the cache and reset browser settings as well. But the issue persists.
badZoke, I was experiencing this same problem yesterday and most of today and finally figured out what was causing it (in my case). This might be helpful for your situation, as well:
Internet Explorer has restrictions about form submission when the form is not currently in the DOM. In my case, a user interacted with a button in a modal popup (which also contained the form). When they clicked, the form was removed from the DOM (but still accessible via a js var) and replaced with a loading bar. I then called myForm.ajaxSubmit(options); which in IE<10 attempts a submission of that form to a temporary <iframe/>. Not allowed unless the form is in the DOM.
If your code above was a simplification of your actual scenario and you were doing something similar to me, this may be your problem. Best of luck.
The first step would be to see if the error callback is invoked:
var options = {
beforeSubmit: function (formData, jqForm, options) {
$(".loaderImage").show();
return true;
},
success: function (responseText, statusText, xhr, $form) {
$("#result").html(responseText);
},
error: function(xhr) { alert(xhr.responseText); }
};
$("#AjaxFileUpload").ajaxSubmit(options);
Your code is working fine on my computer, and I'm using Internet Explorer 9. Maybe the problem is not here.

Facebook Like: Dynamically setting og:title property with JS [duplicate]

I use ajax to render a content page with a Facebook Like Button plugin in it.
The problem is that when the user clics Like, Facebook will extract meta info but I don't know how to assign the meta with ajax.
I tried using append to head int FB.init but it seems to not work and the update isn't reflected when users like the page on Facebook
$('head').append("<meta property="og:title" content="The Rock"/>');
The problem is that facebook like will extract meta info but I don't know how to assign the meta with ajax.
I tried use append to head int FB.init but it seems not work.
Of course this does not work, because Facebook’s scraper requests your URLs from your server – and does not care about what the DOM might currently look like in any user’s browser.
You can not add Open Graph meta data client-side.
Actually you can use such script:
/// Append Meta tags
function setMT(metaName, name, value) {
var t = 'meta['+metaName+'='+name+']';
var mt = $(t);
if (mt.length === 0) {
t = '<meta '+metaName+'="'+name+'" />';
mt = $(t).appendTo('head');
}
mt.attr('content', value);
}
and call this function from body:
setMT('property', 'og:title', 'Title for Facebook');
I have similar on the News Site at http://www.livepage.info
be careful with using '.append()'.
According to the JQuery Docs, this method has a move effect (reads from source, copies to destination and removes the source).
A theoretical way is something like this:
headObj = $("head");
keywordObj = $(headObj).find("meta[name='keywords']");
newKeywords = $(keywordObj).attr("content");
newKeywords += myKeywords;
$(keywordObj).attr("content", newKeywords);
Download and install the plugin FireBug for browser FireFox, so you can check the changes at runtime.

How to Add facebook "Install App Button" on a Fanpage Tab (Based on working example)

For some time now I try to figure out how these guys were able to add "Sign online here" button which is "install App" button on their fan-page tab:
http://www.facebook.com/GuinnessIreland?v=app_165491161181
I've read around the web and couldn't come up with any solid solution. The FBML and FBJS documentation left me with nothing.
So please, if anyone can help me with this.
NOTE: To make multiple tests on the Ajax request that are sent, do not accept the App install. It behaves differently after that.
I had some problems with finding out about it as well. There is no info about this kind of behavior in wiki or anywhere. I got it working after trying some possible solutions and the simplest one is to make user interact with content embedded in fan page tab such as click on something etc. Then you need to post an ajax request with requirelogin parameter to pop up to come out. The simple example would be:
user_ajax = function() {
var ajax = new Ajax();
ajax.responseType = Ajax.RAW;
ajax.requireLogin = true;
ajax.ondone = function(data) {
new Dialog(Dialog.DIALOG_POP).showMessage('Status', data, button_confirm = 'Okay');
}
var url = "http://yourappurl.com/request_handler"
ajax.post(url);
}
And the trigger for it:
Interaction
Then if user is known to be using your application (fan page tab) you can prompt him for additional permission like publish stream or email communication by calling:
Facebook.showPermissionDialog('publish_stream,email');
Hope this solves the problem.

Photos in the Facebook album not showing

I am using Facebook connect and JS client library to retrieve my photos in my Facebook account.
Below is my code:
<div id="photos_pics"></div>
<script type="text/javascript">
var widget_div = document.getElementById("photos_pics");
FB.ensureInit(function () {
FB.Facebook.get_sessionState().waitUntilReady(function() {
FB.Facebook.apiClient.photos_get('12345678', null, null, function(photos,e) {
var markup = "";
for (var i = 0; i < photos.length; i++) {
var photo = photos[i];
var photo_pid = photo.pid;
markup += '<fb:photo size="small" pid="'+photo_pid+'"></fb:photo>';
}
widget_div.innerHTML = markup;
FB.XFBML.Host.parseDomElement(widget_div);
});
});
});
</script>
The below method retrieves the photos:
FB.Facebook.apiClient.photos_get(uid, aid, null, result)
When I only specify uid which is my Facebook id, it works and photos (not in any albums) are returned. But when I specify one of my photo albums in aid, no results returned.
Anyone know what is the error I made? Thanks in advance.
I have recently been having a significant number of problems with retrieving photos from facebook lately as well. Almost always it is revolving around a recent change they have made to pid's. When viewing a photo in facebook you will see two query variables in your url pid and aid.
http://www.facebook.com/home.php#/photo.php?pid=9513212&id=672915455
although this is deceiving because the recent change that facebook made to pid's is that they are actually in the form id_pid so if I were to include the above photo in an fb photo tag it would look like the following
<fb:photo size="small" pid="672915455_9513212"></fb:photo>
Although I am having lots of problem with this because in more than one place facebook is being inconsistent with which form of pid it will return or is looking for.
So my first suggestion is to see which style pid you are receiving and which ones you are trying to use. This is most likely your current problem.
Your next problem is going to be the fact that there is a current bug in the xfbml version of the fb:photo tag. Currently the tag errors out when parsing the new style tags that have an "_" in them. To get around this we simply made a query for the direct url that facebook uses for the image, and display it using a simple html img tag