Forum Moderators: open

Message Too Old, No Replies

Set focus to elementbyid when added via AJAX after page load?

I am unable to give elements focus if they are loaded as AJAX content.

         

JAB Creations

12:53 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before AJAX event...
<div id="loadajaxhere"></div>

After AJAX event...

<div id="loadajaxhere">
<div><a href="#" id="give_focus">example</a></div>
</div>

The after AJAX event displays just a basic example of the initial content with the AJAX loaded content.

I am unable to have JavaScript see an element if the content it's part of is loaded via AJAX.

How do I give AJAX id's focus?

- John

httpwebwitch

1:06 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can load a <script> via AJAX. When it "lands" on your page, it'll be executed.

So, along with "<div><a href="#" id="give_focus">example</a></div>"
send along a "<script>document.getElementById('give_focus').focus()</script>" in the same XHR.

Either that or you hook right into the AJAX response handler, and execute a little focus() job after injecting the HTML onto the page

JAB Creations

1:47 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not putting any script elements in to the body element.

You lost me on what you mean by hooking in to the response handler? I could look stuff up if I had something to reference. ;)

- John

JAB Creations

3:38 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I solved the problem! Works in Gecko, IE, Opera, and Webkit: AJAX with focus for content loaded via AJAX! :)

- John

AJAX Function

ajaxpage(path + 'includes/ajax_content.php', 'id_target','focus_element');

Modified AJAX JavaScript adding focus support!

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid,givefocus){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid,givefocus)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid,givefocus){
if (page_request.readyState == 4 && (page_request.status==200 ¦¦ window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText;

document.getElementById(givefocus).focus();
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}