Skip to content

Instantly share code, notes, and snippets.

@dallarosa
Created December 20, 2010 09:34
Show Gist options
  • Save dallarosa/748200 to your computer and use it in GitHub Desktop.
Save dallarosa/748200 to your computer and use it in GitHub Desktop.
Function for inserting some content (thread) after a marker(needle) inside a container (haystack). haystack should be and object.
function insert_after(haystack, needle, thread)
{
var browser=navigator.userAgent;
if((typeof haystack.innerText == 'undefined') || (!(browser.indexOf("MSIE")>0)))
{
var haystackText = haystack.innerHTML;
}
else
{
var haystackText = haystack.innerText;
}
var index=haystackText.indexOf(needle);
var bgnText = haystackText.substr(0,index+needle.length+1);
var endText = haystackText.substr(index+needle.length,haystackText.length);
if((typeof haystack.innerText == 'undefined') || (!(browser.indexOf("MSIE")>0)))
{
haystack.innerHTML = bgnText + thread + endText;
}
else
{
haystack.innerText = bgnText + thread + endText;
}
}
@dallarosa
Copy link
Author

This function works (tested) with IE8, FF3.6, Safari 5, Chrome 8 on Linux and Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment