Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Yarly

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Find element position relative to other elements

http://particletree.com/notebook/finding-element-positions/

Insert in place without document.write

// http://www.sitepoint.com/blogs/2007/07/11/insert-in-place-without-documentwrite/

// So, given a syndication script with a fixed ID:
<script type="text/javascript" id="syndication" src="syndication.js"></script> 

// We can go from oldskool nastiness like this:
document.write('<p id="syndicated-content">Here is some syndicated content.</p>'); 

// To modern loveliness like this:

var newcontent = document.createElement('p'); 
newcontent.id = 'syndicated-content'; 
newcontent.appendChild(document.createTextNode('Here is some syndicated content.')); 
var scr = document.getElementById('syndication'); 
scr.parentNode.insertBefore(newcontent, scr); 

// We could even go a step further and remove the <script> ID, but in that case we would need a concrete method for identifying the specific element. We could do that by knowing its SRC:

var scripts = document.getElementsByTagName('script'); 
for(var i=0; i<scripts.length; i++) 
{ 
  if(scripts[i].src == 'http://www.mydomain.com/syndication.js') 
  { 
    //scripts[i] is the one 
    break; 
  } 
}

Safari <label> fix

if (navigator.userAgent.indexOf('Safari') != -1) {
        window.addEventListener('load', function() {
                var lock = false;
                var labels = document.getElementsByTagName('label');
                for (var i = 0; i < labels.length; i++)
                        labels[i].addEventListener('click', function() {
                                var input = (this.htmlFor ? document.getElementById(this.htmlFor) : this.getElementsByTagName('input')[0]);
                                if (input && !lock) {
                                        input.focus();
                                        lock = true;
                                        input.click();
                                        lock = false;
                                }
                        });
        });
}
« Newer Snippets
Older Snippets »
3 total  XML / RSS feed