/    Sign up×
Community /Pin to ProfileBookmark

The first: I’m trying to do a check to see if a certain string of text is on the webpage (not html but the page itself) but I’m not sure on how to check it. I need a variable or something in which the page content is held. This way is the only way I can think of but it doesn’t work.

[CODE]window.document.body.indexOf(‘watch in high quality’);[/CODE]

The second: OnLoad isn’t working. So far I have this code:

[code]
function changeQ() {alert(‘Hello’);}

if (document.addEventListener) {
document.addEventListener(“Load”, changeQ, false);

document.onLoad = alert(‘Hello’);
[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 21.2008 — JavaScript is case-sensitive, load is not the same as Load.
<i>
</i>function changeQ() {
if (document.getElementsByTagName("body")[0].innerHTML.indexOf("string") &gt; -1) {
alert("Here");
}
else {
alert("Not here");
}
}

if (document.addEventListener) {
window.addEventListener("[color=red]l[/color]oad", changeQ, false);
}
else {
window.on[color=red]l[/color]oad = alert('Hello');
Copy linkTweet thisAlerts:
@tenfoldauthorJun 21.2008 — Thanks. Works now.

For the sake of making another post, would you know why this line of code doesn't center the div assigned to this ID?

document.getElementById('watch-this-vid').style.align = 'center';
Copy linkTweet thisAlerts:
@FangJun 21.2008 — There is no style align. You are thinking of the attribute align, which is depreciated.
Copy linkTweet thisAlerts:
@Declan1991Jun 21.2008 — <i>
</i>var d = document.getElementById('watch-this-vid');
d.style.margin = 'auto';
d.parentNode.textAlign = "center";

The text-align is necessary for one of the earlier IEs. Exclude it if the HTML doesn't suit.
Copy linkTweet thisAlerts:
@tenfoldauthorJun 21.2008 — How would I wrap center tags around the div start & end then?
Copy linkTweet thisAlerts:
@Declan1991Jun 21.2008 — You don't have to.document.getElementById('watch-this-vid').style.margin = 'auto';
will do it for for everything but IE 5.5 and lower. If you want to support IE 5.5, add this line:document.getElementById('watch-this-vid').parentNode.textAlign = "center"; Better again would probably be this<i>
</i>var d = document.getElementById('watch-this-vid');
var newd = document.createElement("div");
var newi = d.cloneNode(true);
newi.style.margin="auto";
newd.style.textAlign = "center";
newd.appendChild(newi);
d.parentNode.replaceChild(newd,d);
That centers the div and the text. If you only want to center the div not the text, use this instead.
document.getElementById('watch-this-vid').parentNode.textAlign = "center"; Better again would probably be this<i>
</i>var d = document.getElementById('watch-this-vid');
var newd = document.createElement("div");
var newi = d.cloneNode(true);
newi.style.margin="auto";
newi.style.textAlign = "left";
newd.style.textAlign = "center";
newd.appendChild(newi);
d.parentNode.replaceChild(newd,d);
Copy linkTweet thisAlerts:
@Angry_Black_ManJun 22.2008 — as for your first question that hasnt been considered, i have been trying quite fruitlessly to create a DOM walking script:

&lt;br&gt;&lt;hr&gt;

&lt;div&gt;text1&lt;p&gt;text2&lt;/p&gt;text3&lt;/div&gt;

text4

&lt;script&gt;
// adapted from erroneous code from:
// http://books.google.com/books?id=VOS6IlCsuU4C&amp;pg=RA1-PA319&amp;lpg=RA1-PA319&amp;dq=traverse+textnodes+javascript&amp;source=web&amp;ots=wHNbBRcxyX&amp;sig=gpNR3-OTkvQK0lLw07vYqPQcPgA&amp;hl=en&amp;sa=X&amp;oi=book_result&amp;resnum=5&amp;ct=result

numNodes = 0

walkit(document.body, "#text")

function walkit(el, nodeName)
{
if(el.nodeName == nodeName)
numNodes++ // instead of counting, this could check to see if the nodeValue equals a specific search string

<i> </i>if(el.childNodes.length)
<i> </i> for(i = 0; i &lt; el.childNodes.length; i++)
<i> </i> walkit(el.childNodes[i], nodeName)

<i> </i>alert("current number of '" + nodeName + "' nodes -&gt; " + numNodes)
}

&lt;/script&gt;


for some asinine reason i cant get this thing to count the "text4" node.
×

Success!

Help @tenfold spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...