Getting information from a hidden IFrame

D

Drew

This might beyond the scope of this group because it deals with
SharePoint, but I'm not sure if I can't get it to work because of
SharePoint or because JavaScript is weird (I don't have much experience
with JavaScript).

Anyway, here is the code i'm working with. Basically, it creates
appends a hidden IFrame to the page, gets the links from the IFrame,
and displays those which have the "OnLink" property. The weird thing
is that when I uncomment out the alert("x=0") line, it works fine. If
I don't have an alert, then nothing will be written to the page (the
page already exists, I'm adding the JavaScript to the page as a Content
Editor Web Part).

var links;
var hiddenIframe = document.createElement("IFRAME");
var linksArray = new Array();
var x = null;

hiddenIframe.id = "iLinks";
hiddenIframe.style.display = "none";
hiddenIframe.src = "source of links";
WebPart_WPQ_.appendChild(hiddenIframe);

links = frames["iLinks"].document.getElementsByTagName("A");
x = 0;
//alert("x=0");

for (var i=0; i<links.length; i++)
{
var templink = links(i);
var onValues = templink.onclick + templink.onfocus + "";

if(onValues.indexOf("OnLink") >= 0)
{
linksArray[x] = templink;
x++;
}
}



for (var j=0; j<linksArray.length; j++) {
document.write(linksArray[j] + '<br>');
}



I also tried an alternate solution where I called everything function
on IFrame onLoad, but that deleted everything on the page and wrote the
text instead of writing the text to the specific area where the
function was located (as it does when the alert("x=0") is uncommented
on the above code).

Thanks in advance for any help.
 
A

ASM

Drew a écrit :
with the complet function or an example in line
it would have been better ...

perhaps you need new iframe finish to load the file "source of links" ?

hiddenIframe.onload = function() {
// don't forget to close this function at end of script
links = frames["iLinks"].document.getElementsByTagName("A");
x = 0;
//alert("x=0");

for (var i=0; i<links.length; i++)
{
var templink = links(i);
var onValues = templink.onclick + templink.onfocus + "";

if(onValues.indexOf("OnLink") >= 0)
{
linksArray[x] = templink;
x++;
}
}
for (var j=0; j<linksArray.length; j++) {
document.write(linksArray[j] + '<br>');
}

var txt = '';
for (var j=0; j<linksArray.length; j++) txt += linksArray[j] + '<br>';
document.write(txt); // that would have to write in hidden iframe

} // supposed end of iframe's onload function

WebPart_WPQ_.appendChild(hiddenIframe);

}


the onload is to be set before the appendChild

and ... specific areas ... how to consider them with a bit of code as
given ?

you want that write on main page ?
parent.document.write(txt); // would have to write in main page

to write (or add) to div 'someWhere' on main page :
parent.document.getElementById('someWhere').innerHTML = txt;
 
D

Drew

that actually was the complete function. the rest of the page is
automatically created by sharepoint, which i have no control over it
(which is why we're trying to use scripts so we can get more out of
sharepoint). the original code given will write directly to the parent
page, which is what i want. here is the alternate script was referring
to:

<script language="JavaScript">
var links;
var linksArray = new Array();
var contentsArray = new Array();
var x = null;


function getLinks()
{
links = frames["iLinks"].document.getElementsByTagName("A");

x = 0;

for (var i=0; i<links.length; i++)
{
var templink = links(i);
var onValues = templink.onclick + templink.onfocus + "";


if(onValues.indexOf("OnLink") >= 0)
{
linksArray[x] = templink;
contentsArray[x] = links(i).innerHTML;
x++;
}
}
writeLinks();
}

function writeLinks()
{
for (var j=0; j<linksArray.length; j++)
{
parent.document.write(contentsArray[j].link(linksArray[j]) +
'<br>');
}
}

function mainFunc()
{
var hiddenIframe = document.createElement("IFRAME");
hiddenIframe.attachEvent("onload", getLinks);
hiddenIframe.id = "iLinks";
hiddenIframe.style.display = "none";
hiddenIframe.src =
"http://teamsites.wachovia.net/it/co...tCenter/testcenter2/Lists/Links/AllItems.aspx";
WebPart_WPQ_.appendChild(hiddenIframe);
}

window.attachEvent("onload", mainFunc);
</script>




this script, however, will clear the entire sharepoint page and write
the first link of the array.
Drew a écrit :
with the complet function or an example in line
it would have been better ...

perhaps you need new iframe finish to load the file "source of links" ?

hiddenIframe.onload = function() {
// don't forget to close this function at end of script
links = frames["iLinks"].document.getElementsByTagName("A");
x = 0;
//alert("x=0");

for (var i=0; i<links.length; i++)
{
var templink = links(i);
var onValues = templink.onclick + templink.onfocus + "";

if(onValues.indexOf("OnLink") >= 0)
{
linksArray[x] = templink;
x++;
}
}
for (var j=0; j<linksArray.length; j++) {
document.write(linksArray[j] + '<br>');
}

var txt = '';
for (var j=0; j<linksArray.length; j++) txt += linksArray[j] + '<br>';
document.write(txt); // that would have to write in hidden iframe

} // supposed end of iframe's onload function

WebPart_WPQ_.appendChild(hiddenIframe);

}


the onload is to be set before the appendChild

and ... specific areas ... how to consider them with a bit of code as
given ?

you want that write on main page ?
parent.document.write(txt); // would have to write in main page

to write (or add) to div 'someWhere' on main page :
parent.document.getElementById('someWhere').innerHTML = txt;
 
D

Drew

I finally figured it out. I think the problem was in the way
SharePoint was set up, it may have worked under different
circumstances. Here is my way around it though, if anyone is
interested:



<script language="JavaScript">
var links;
var linksArray = new Array();
var contentsArray = new Array();
var x = null;
var tbl = document.createElement("TABLE");
var tbody = document.createElement("TBODY");



function getLinks()
{
links = frames["iLinks"].document.getElementsByTagName("A");

x = 0;

for (var i=0; i<links.length; i++)
{
var templink = links(i);
var onValues = templink.onclick + templink.onfocus + "";


if(onValues.indexOf("OnLink") >= 0)
{
linksArray[x] = templink;
contentsArray[x] = links(i).innerHTML;
x++;
}
}
writeLinks();
}

function writeLinks()
{
for (var j=0; j<linksArray.length; j++)
{
var tr = document.createElement("TR");
var td = document.createElement("TD");
var aLink = document.createElement("A");
aLink.setAttribute("href", linksArray[j]);
aLink.appendChild(document.createTextNode(contentsArray[j]));
td.appendChild(aLink);
tr.appendChild(td);
tbody.appendChild(tr);
}

tbl.appendChild(tbody);
WebPart_WPQ_.appendChild(tbl);
}

function mainFunc()
{
var hiddenIframe = document.createElement("IFRAME");
hiddenIframe.attachEvent("onload", getLinks);
hiddenIframe.id = "iLinks";
hiddenIframe.style.display = "none";
hiddenIframe.src = "URL of links list";
WebPart_WPQ_.appendChild(hiddenIframe);
}


window.attachEvent("onload", mainFunc);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top