Archive for the ‘JavaScript’ Category

Posted by 6bytes at 14, December, 2012

Category: JavaScript

Tags: ,

This is just a quick tip how to find out which version of jQuery is loaded.
It can come in handy for example when you’re developing a script for an existing site and you need to find out which version of jQuery they use. Of course you can always just look in the HTML code, but this method is not always 100% reliable.

$().jquery;
// Returns "1.7.1"

or

jQuery.fn.jquery;
// Returns "1.7.1"

Posted by 6bytes at 15, January, 2012

Category: JavaScript, WordPress

Tags: , , , ,

I wanted to add Google AdSense to my blog just to see how easy it is to do. I’ve googled a bit and found a handful of plugins that claim to do it all for you. I couldn’t decide which one to choose and started thinking from a different angle. It turns out that you don’t need any plugins to install Google AdSense on your site and what’s most important you don’t even need any programming knowledge! The whole process will take you no more than 5 minutes.

  1. Log in to your blog’s admin panel.
  2. Click on Appearance.
  3. Click on Widgets.
  4. Find a Text widget and drag it to the Sidebar (or any other place where you want the AdSesnse to appear).
  5. Enter title. I’ve used “Interesting Links”.
  6. Paste your Google AdSense code into widget’s content area. My code looks like this:
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-4589490261258499";
    /* allurcode */
    google_ad_slot = "2642741863";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    
  7. Click Save.

That’s it, we’re done! Go to your blog’s homepage and smile 🙂

Note. Allow a few minutes for Google to create the adds before they actually show up.

Posted by 6bytes at 30, June, 2011

Category: JavaScript

Tags: , ,

Just a quick tip, how to check if current page has been loaded in an iframe or was it accessed directly.

if (window.self === window.top) {
    // we're NOT in an iframe
} else {
    // we're in an iframe
}

That’s it.

Posted by 6bytes at 1, March, 2011

Category: JavaScript

Tags: , , ,


flowplayer is an Open Source (GPL 3) video player for the web. Allows you to easily embed video streams into your web pages.
While it’s easy to integrate and very powerful occasionally it may give you headaches especially with cross-browser compatibilities.

The most basic configuration looks like this:

	<script type="text/javascript" src="/js/flowplayer-3.2.6.min.js"></script>
	<a id="player" href="/path/to/movie/example_movie.flv"></a>
<script>
	flowplayer("player", "/path/to/file/flowplayer-3.2.6.swf");
</script>

So you’ve done everything according to documentation and your flowplayer works fine in Firefox but not in Internet Explorer and/or Google Chrome.

There’s a couple of things that worked for me:

1. Change Flash player and your movie URL’s from relative to absolute.

	<a id='player' href='http://www.example.com/path/to/movie/example_movie.flv'></a>
	flowplayer('player', 'http://releases.flowplayer.org/swf/flowplayer-3.2.6.swf');

2. Make sure that JavaScript and Flash files are the same version. In our case 3.2.6

3. Edit flowplayer’s JavaScript library flowplayer-3.2.6.min.js (even the minified file). Look for 9,0 and change it to 10,0. This ensures that flowplayer requests Flash player 10 installed in your browser (latest version by the time this post was writen).
OR request a Flash version in your flowplayer start script:

	flowplayer('player', {
		src: 'http://releases.flowplayer.org/swf/flowplayer-3.2.6.swf',
		version: [10, 0],
		onFail: function()  {
			// Write your own message for devices not supporting Flash
		}
	},
	{
		clip: 'http://www.example.com/path/to/movie/example_movie.flv'
	});

Posted by 6bytes at 30, September, 2010

Category: JavaScript

Tags: ,

In my last project I needed a function in JavaScript that will execute itself as soon as it’s ready. My first idea was something like that:

// declare function
function myFunction() {
	alert('Hello world!');
}
// run it when DOM is ready with help of jQuery
$(document).ready(function() {
	myFunction();
});

but in the back of my mind I knew there is a better way. A little bit of googling resulted in something unbelievably simple like this:

(function() {
	alert('Hello world!');
})();

If you need you can pass arguments as you’d normally do:

(function( arg ) {
	alert('Hello world! '+arg);
})(9999);

Quick and simple 🙂

Posted by 6bytes at 30, August, 2010

Category: JavaScript

Tags: , , , , , ,

Recently I’ve been working on a project with HTML5 <video> tag event handling and I needed to check if an element exists in the array. Quick googling resulted in neat native JavaScript method Array.indexOf. It returns the first index at which a given element can be found in the array, or -1 if it is not present. It all worked just fine in Firefox, Chrome and such.
Why wasn’t I surprised when my script behaved a bit weird in Internet Explorer. After many hours of “fun” with IE JavaScript debugger, I found that none of IE’s support the Array.indexOf method.

Thanks to this post I was able to quickly fix it by adding below code to my script.

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]===obj){
				return i;
			}
		}
		return -1;
	}
}

and the world was beautiful again.

 

Posted by 6bytes at 19, April, 2008

Category: JavaScript

Tags:

Over the years I used a lot of JavaScript functions to open PopUp windows. Some time ago I wrote this simple function and I use it everywhere where I need to open a new window.
Add the following to your JavaScript file or embed it in the tag somewhere on your website:

function popUp(strURL, strName, strType, strTop, strLeft, strWidth, strHeight) {
var strOptions = "";
if (strType == "console") strOptions = "resizable, top="+strTop+", left="+strLeft+", width="+strWidth+", height="+strHeight;
if (strType == "fixed") strOptions = "status, top="+strTop+", left="+strLeft+", width="+strWidth+", height="+strHeight;
if (strType == "elastic") strOptions = "toolbar, menubar, scrollbars, resizable, location, top="+strTop+", left="+strLeft+", width="+strWidth+", height="+strHeight;
var newWin = window.open(strURL, strName, strOptions);
newWin.focus();
}

and to run it simply write:

your link

You need to create file.php file first. You can easily pass variables to this file like that:

your link

Enjoy!