PDA

View Full Version : open new page plus call function



sketch
07-29-2010, 04:08 AM
Hi
Just a quick question..hopefully a simple one for you guys.

I have 6 pages in a site im building, with hidden <div> tags. In each of these pages there are links to change the display property of specific divs.
This all works fine.

The issue is, that from the Sitemap in the footer, I want the links to open the new page AND change the display property of a specific div to "block"


I hope that makes sense. If you need more info just let me know.

Thanx in advance for any suggestions.

jscheuer1
07-29-2010, 05:00 AM
Let's say the page you are opening is info.htm, and the function you want to run is a simple alert. The link to that page could be like:


<a href="info.htm?var1=bob">Whatever</a>

Then on info.htm you could have:


<script type="text/javascript">
function getQval(n) {
if(typeof n !== 'string'){
return null;
}
var r = new RegExp('[?&;]' + n + '=([^&;#]*)'), m = location.search;
return (m = r.exec(m))? unescape(m[1]) : null;
}

if(getQval('var1')){
alert(getQval('var1'));
}
</script>

In this case it would alert 'bob'. I think you can see how this could be used to achieve your objective. If not:

Please post a link to a page on your site that contains the problematic code so we can check it out.

sketch
07-29-2010, 05:21 AM
Sorry.. im quite competent with javascript, but maybe not THAT competent.

heres the scenario..

each page has hidden divs (display:none) with links to call a function that changes that to (display:block)
these work fine from within the page itself.

What i need is from the sitemap to open the new page and call the function to change display:block.

eg.. sitemap
Networking (main page)
-network design (link on page)
-network setup (link on page)
-network security (link on page)

when i click on Networking, it will open the page "network.asp"
..BUT.. when i click say, "network setup" i want it to open network.asp page, AND set display:block to the "networksetup" div.

In effect, opening the right page, and showing the right content from each link in the sitemap.

jscheuer1
07-29-2010, 06:53 AM
Do you have the script to set the display on the desired division? If not, try:

http://www.dynamicdrive.com/dynamicindex17/animatedcollapse.htm

especially its 6th listed ability under its description heading:


6. Ability to expand the DIVs via a URL parameter string that overrides any default settings/persistence.

as explained in detail here:

http://www.dynamicdrive.com/dynamicindex17/animatedcollapse_suppliment2.htm

sketch
07-30-2010, 05:30 AM
yeah I already have the code for the divs to show and hide. all working.

I actually did have a look at that link you posted before, but i found that after I had got the functions to work on the divs now. And time is running out so i'd rather not change it.

Which is why im hoping there is some way to open the new page with the function call to show the specified div.

jscheuer1
07-30-2010, 04:04 PM
Well, just use my example link and code from my first post in this thread. Only instead of doing:


if(getQval('var1')){
alert(getQval('var1'));
}

do whatever it takes to expand that div. I don't know because I don't have your code. Lets say your expand function is called expandit.

You would want something like so:


onload = function(){
if(getQval('var1')){
expandit(getQval('var1'));
}
}

Or, if you want to be sure it doesn't conflict with other onload events:


if(getQval('var1')){
(function(){
function init(){
expandit(getQval('var1'));
}
if (window.addEventListener){
window.addEventListener('load', init, false);
}
else if (window.attachEvent){
window.attachEvent('onload', init);
}
})();
}

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.

sketch
08-01-2010, 01:40 AM
the function just hides all divs with specific "name" and shows the one with the specified "id".



function showmain(pass) {
var divs = document.getElementsByAttribute('name', 'main_content');
for(i=0;i<divs.length;i++){
if(divs[i].id.match(pass)){
if (document.getElementById)
divs[i].style.display="block";
else
if (document.layers)
document.layers[divs[i]].display = 'block';
else
document.all.divs[i].display = 'block';
} else {
if (document.getElementById)
divs[i].style.display="none";
else
if (document.layers)
document.divs[i].display = 'none';
else
document.all.divs[i].display = 'none';
}
}
}


when i call the function, it works like a charm, from within the page...i need to keep that working, but also to work when opening a new page.
If you have a look at the site, down the bottom in the Sitemap, thats where i need help with. WEBSITE (http://www.icgdesigns.site90.com/network)

Thanx again for your help, i really appreciate it.

mburt
08-01-2010, 02:24 AM
So basically you just need two things to happen within one call? And assuming you've already made a function with changes a DIV's style, display property, just add this:


<a href="networkingpage.htm" target="_blank"><span onclick="showdiv('network');">Networking</span></a>

It may be a little redundant but it should work.

sketch
08-01-2010, 02:41 AM
that looks like it would show the apropriate div, in the current page..changing _target to self, will simply open the page, but the function wont be called.

what i need is to open "network.html" and within that page to call the function to show the specified div.
so for eg... say im on home.html, and i want to show "networkdesign" div. which isnt in home.html, its located in network.html.
When i click on Network Design link in the sitemap, I need two things to happen.
1 - open "network.html" page.
2 - call function showmain("networkdesign")

read my second post, i described it better in there.

mburt
08-01-2010, 02:54 AM
Ah okay, you need to incorporate something in the URL then.

Like: "newpage.php?div=certain_div"

Then within newpage.php, call that value.


<script type="text/javascript">
window.onload = function() {
showdiv("<?php echo $_GET["div"]; ?>");
}
</script>

sketch
08-01-2010, 04:06 AM
perfect!!! thanx alot dude. thats just what i wanted.

one thing tho..from the sitemap with "new_page.html?div=specific_div" this reloads the same page.
for eg.. if im on network.html with "network design" div showing, and i click on "network setup", it will reload network.html, then show "network setup" div.

its nothing serious..just wondering if theres a way around that?

only thing i can think of for now, is to change links on the "current" page to simply call the function.
for eg.. on network.html page, change sitemap links related to this page, to call the function alone.

seems a little tedious...so im just wondering if theres a better more productive way to do it?

*** UPDATE *****
I just found something wrong now... when i open a new page, not specifying "?div=specific_div" ...the page loads and shows all hidden divs. ??
would a "if" statement suffice in the onload function?

mburt
08-01-2010, 05:09 AM
You have to make sure that the code on the parent page to HIDE all of the DIV's is on the NEW page as well, to make sure they stay hidden when it loads.

As for the page re-naming thing, it may be slightly monotonous but if it works just invest the time.

sketch
08-01-2010, 10:06 AM
the divs are hidden by default. thats why i dont understand why this is happening.
and, yes, i have the code on all pages.