Posts Tagged JavaScript

JavaScript redirect based on referrer

Basic stuff (as often the case not immediately findable). You want a simple script to redirect to a different page if te referrer is such and such. In the following example the two referral page options ( foo.html and bar.html ) are separated with the double pipe (the ‘or’ bit)…

<script type="text/javascript">
if (document.referrer == ("http://example.com/foo.html" || "http://example.com/bar.html")) 
      location.href = "foobar.html"; 
</script>

,

No Comments

jQuery in anger

[notes to self]

Shadowbox

Tooltip

highlightFade

http://jquery.offput.ca/highlightFade/old.php

jquery.validate.js

Lovely, lightweight carousel:
http://www.gmarwaha.com/jquery/jcarousellite/

http://www.noupe.com/jquery/50-amazing-jquery-examples-part1.html

http://www.jasonbradbury.com/

, , ,

No Comments

Avoid HTML validation errors due to your JavaScript

One of the reasons I changed a random content rotation script that was sitting in my <head> tags last month was because this JavaScript failed to validate. I don’t intend to change back – but today I found out the why-for when I came across the same thing happening in two versions of “The Son of Suckerfish”. One wrapped the JavaScript with CDATA tags the other not. That simple. Of course removing the script into its own file would do the trick too.

Don’t forget to wrap up your script tags safely!

<script type="text/javascript"><!--//--><![CDATA[//><!--
PUT YOUR CODE IN HERE
//--><!]]></script>

This applies to all XHTML pages.

, , ,

No Comments

Options for “hybrid modal” lightbox display of images

[Update 2009 - OK now I'm not the total newbie anymore and would have to recommend Shadowbox on top of jQuery for it's versatility and power. Slimbox is still great though.]


The Contenders

Slimbox

The true successor to the king (Lightbox). This is the chosen one. Very lightweight, simple instructions, does what it says on the tin. Simple instructions for when I’ve forgotten in two weeks are further down

Shadowbox.js

This one seems to be the current best in breadth of options but too much to read for a low level webdev monkey like me.

Lightbox

The original – but perhaps no longer the beast.

Thickbox

Could have been a contender but I balked at the heavy graphic design.

FancyZoom

The one that started the search thanks to seeing it in action at this FontFeed article. Great tool – but as a webdev guerilla [read chimp] the commercial licence stopped me. Need a bit of slack before I can convince anybody to pay…


Slimbox walk-through

Upload css and js files – add these three lines of code to the head of the HTML:

<script type="text/javascript" src="js/mootools.js"></script>
<script type="text/javascript" src="js/slimbox.js"></script>
<link rel="stylesheet" href="css/slimbox.css" type="text/css" media="screen" />

(The last slash there may want removing according to your doctype. Are you strict enough for it?)
Then craft the images links like this for single images

<a href="" rel="lightbox" title=""><img src="" alt="" /></a>

or, for sets, like this (where the rel attribute has the set name’s suffix)

<a href="" rel="lightboxset" title=""><img src="" alt="" /></a>

, , , , ,

No Comments

InDesign CS2 export script

This very useful JavaScript code was developed by Peter Kahrel as he found my problem interesting. Bless you Peter! The full thread is over on the Adobe’s InDesign Scripting Forum here abouts. Because there’s only a limited life for any post over there – I’m dumping here too for posterity.

The purpose of the script is to export single page PDFs from a multi-page InDesign CS2 document where the page numbering has been set via page Numbering and Section Options. PDFs are exported to a predetermined PDF setting: “PDF_X-1a with Bleed & co” in my case. The resulting file PDFs are named with a three-digit prefix corresponding to the page number in the InDesign document and a user defined base name – defined when the script is run.

A basic version of this script is supplied by Adobe in with the extras for an InDesign install but unfortunately that falls over when there is a Named Section present. Getting a stray Named Section in your document is very easy. Easier for some users mind – but still very easy. Peter – I still owe you a beer or three…

Here is the magic script:

#target indesign 

if(app.documents.length != 0) 
   { 
   var myFolder = Folder.selectDialog ("Choose a Folder");   
   if(myFolder != null) 
      { 
      myExportPages(myFolder); 
      } 
   } 
else 
   { 
   alert("Please open a document and try again."); 
   } 

function myExportPages(myFolder) 
{ 
var myPageName, myFilePath, myFile; 
var myDocument = app.activeDocument; 
var myDocumentName = myDocument.name; 
//This next line sets the PDF export Setting 
var myPDFExportPreset = app.pdfExportPresets.item("PDF_X-1a with Bleed & co") 
var myDialog = app.dialogs.add(); 
with(myDialog.dialogColumns.add().dialogRows.add()) 
   { 
   staticTexts.add({staticLabel:"Base name:"}); 
   var myBaseNameField = textEditboxes.add({editContents:myDocumentName, minWidth:160}); 
   } 
var myResult = myDialog.show({name:"ExportPages"}); 
if(myResult == true) 
   { 
   var myBaseName = myBaseNameField.editContents; 
   myDialog.destroy(); 
   // Remember the state of each section prefix 
   var section_states = myDocument.sections.everyItem().includeSectionPrefix; 
   // Get all section-prefix names 
   var section_names = app.activeDocument.sections.everyItem().name; 
   // Enable all section prefixes 
   myDocument.sections.everyItem().includeSectionPrefix = true; 
   // Create two parallel arrays with page numbers: 
   // one with numbers preceded by section prefixes, 
   // the other with bare page numbers 
   var page_ranges = []; 
   var filename_pagenames = []; 
   for(var i = 0; i < myDocument.pages.length; i++) 
      { 
      page_ranges.push (myDocument.pages.item(i).name); 
      var temp = strip_section (myDocument.pages[i].name, section_names); 
      filename_pagenames.push (("00" + temp).slice(-3)) 
      } 
   // Restore the section-prefix settings 
   restore_section_states (myDocument, section_states); 
   // Export each page 
   for(var i = 0; i < myDocument.pages.length; i++) 
      { 
      app.pdfExportPreferences.pageRange = page_ranges[i]; 
      myFilePath = myFolder + "/" + filename_pagenames[i] + "-" + myBaseName + ".pdf"; 
      myFile = new File(myFilePath); 
      myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset); 
      } 
   } 
else 
   { 
   myDialog.destroy(); 
   } 
} 

function restore_section_states (doc, states) 
{ 
for (var i = 0; i < doc.sections.length; i++) 
   doc.sections[i].includeSectionPrefix = states[i]; 
} 

function strip_section (pg, array) 
{ 
for (var i = 0; i < array.length; i++) 
   if (array[i].length > 0) 
      if (pg.match (RegExp ('^'+array[i])) != null) 
         { 
         pg = pg.replace (RegExp ('^'+array[i]), ''); 
         break 
         } 
return pg 
}

, , ,

1 Comment