Archive

Archive for September, 2012

Use jQuery to include a include a Twitter feed

This snippet is designed for mobile apps, but with a suitable proxy, it would work on websites too.

<html>
<head>
<script src=”jquery.js”></script>
<script language=”javascript”>
$(init);
function init()
{
$.get(‘http://api.twitter.com/1/statuses/user_timeline.json?screen_name=petruccimusic&#8217;, function(data) {
var strHtml = “<ul>”;
for(var i in data)
{
var strText2 = replaceURLWithHTMLLinks(data[i].text);
strHtml += “<li>” + strText2 + “</li>”;
}
strHtml += “</ul>”;
$(“.result”).html(strHtml);
});
}
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
return text.replace(exp,”<a href=’$1′>$1</a>”);
}
</script>
</head>
<body>
<div class=”result”></div>
</body>
</html>

 

Categories: Uncategorized

iPad: Splash screen appearing in top left hand corner while loading

On the iPad, if you’re developing on PhoneGap, then you may have seen this problem, where the main splash screen appears, then disappears moments later showing a smaller splash screen in the top left hand corner, partially obscuring content, then disappearing again a few moments later, like this app:

Then you may be surprised with a simple solution, remove the splash/default.png image. This has the side effect of having no splash at all on the iPhone version, but apparently Apple discourage splash screens anyway.

 

Categories: Uncategorized

Read Cookies from another domain using Javascript and ASP.NET

Reading cookies from another domain is a security risk, in terms of session hijacking, however, there may be reasons why you may want to do this.

Here is a simple technique that allows Cookies to be read from one domain that have been set on another domain.

First, add an ASPX page with the content shown below, and save it as “xdom.aspx” in the root of your domainĀ (“domain-A”)

 

var pairs = “<% Response.Write(Request.ServerVariables[“HTTP_COOKIE”]);%>”.split(“;”);
var cookies = {};
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split(“=”);
cookies[pair[0]] = unescape(pair[1]);
}

 

Then add a html file to another domain (“domain-B”)

 

<script src=”http://domain-A/xdom.aspx”></script&gt;
<script language=”javascript”>
console.log(cookies);
</script>

Running this in Google Chrome, open developer console, and then expand out “object” and you’ll see the cookies that were set on “domain-A”

Categories: Uncategorized

CloudCarousel: Rotating diagonally

A nice carousel effect with Javascript can be achieved with “Professor cloud’s cloudcarousel” (http://www.professorcloud.com/mainsite/carousel-test.htm), This carousel rotates in an ellipsisĀ horizontally, Ā by making a small change it is possible to rotate this carousel 45 degrees, so that it rotates diagonally.

First the maths, where I plotted an askew ellipsis with Wolfram | Alpha, as follows:

x= cos(t)cos(1.1)-sin(t)sin(1.1), y=0.5(cos(t)sin(1.1)-sin(t)cos(1.1))

Where 1.1 is the rotation in Radians, and the 0.5 determines how high

and low the carousel should rotate

 

 

 

 

 

 

To translate this into code, I edited the first few lines of the controller “class” as follows:

 

var Controller = function(container, images, options)
{
var items = [];
var funcSin = function(t){ return 2.2*(Math.cos(t)*Math.sin(2) – Math.sin(t)*Math.cos(2)) };
var funcCos = function(t){ return Math.cos(t)*Math.cos(2) – Math.sin(t)*Math.sin(2) };
var ctx=this;

Categories: Uncategorized

‘undefined’ is not an object (evaluating ‘d[0][a.expando]’)

I came across a very unusual error with JQuery Mobile (JQM), where I had a few listviews added to a page dynamically, then wanted to initialize them with the $(“.ulXYZ”).listview(); command, this was throwing a cryptic exception:

‘undefined’ is not an object (evaluating ‘d[0][a.expando]’

Which resulted in the first listview on the page not being initialized

My solution, catch the error, and attempt a refresh a second later:

try
{
// Result list may not be yet initialized
$(“.ulXYZ”).listview();
}
catch(e){
console.log(“failed to refresh listview:” + e)
setTimeout(function(){
$(“.ulXYZ”).listview(“refresh”);
console.log(“Attempting refresh”); },1000);
};

Would love to know what the underlying cause of the problem is, but this fixes it after a second delay.

Categories: Uncategorized