This is a discussion on "problem with main page when opening new window" within the JavaScript Forum section. This forum, and the thread "problem with main page when opening new window are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
problem with main page when opening new window
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
problem with main page when opening new window
Hello,
im very new to webdesign and this is my first go with javascript. i managed to create rollover image link that opens a page in a new, smaller window: <a href="" onClick="window.open('newpage.html','mywindow','wi dth=400,height=200')" onmouseover="MM_swapImage('Image1','','mypic.gif', 1)" onmouseout="MM_swapImgRestore()"><img src="mypic2.gif" name="Image1" width="56" height="27" border="0" id="Image1" /></a> The window opens fine, BUT: the main page, where the link is placed, scrolls back up to the top.... Is this normal? i used a tutorial to create the code, (but had to mix contents of different chapters to get what i wanted). And the problem doesnt occure on the tutorial page, when clicking the example. (so i think there must be a way, somehow....) is there something wrong with my code? or do i have to add another line to prevent the page from scrolling back up? im pretty helpless at the moment. can someone help me? |
|
|
|
#2
|
||||
|
||||
|
Re: problem with main page when opening new window
That's because you left out the href="" attribute, so the links points to the top of the current page.
Add the code in red to your link and the browser won't actually follow the link. <a href="" onClick="window.open('newpage.html','mywindow','wi dth=400,height=200'); return false;" onmouseover="MM_swapImage('Image1','','mypic.gif', 1)" onmouseout="MM_swapImgRestore()"><img src="mypic2.gif" name="Image1" width="56" height="27" border="0" id="Image1" /></a>
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#3
|
|||
|
|||
|
Re: problem with main page when opening new window
I hope you aren't leaving your code like this. Everything should be external and using a class in any links you want to poup and getElementsByTagName this is easy to achieve. And why not use css for your rollover?
This is a bit obtrusive for me and there is no fall back. At the very least put the popup link in as the href as well. This way the link has not lost all of its functionality if JS is off (a searchbot for example). Pete. |
|
#4
|
||||
|
||||
|
Re: problem with main page when opening new window
Quote:
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#5
|
|||
|
|||
|
Re: problem with main page when opening new window
thanks spinal.
i filled the link into the href= |
|
#6
|
|||
|
|||
|
Re: problem with main page when opening new window
oops, what happened to the rest of the post...?:
thanks spinal. i filled the link into the href="" aswell. and thanks again, just wanted to say, i dont even understand half of the post dont understand this: "Everything should be external and using a class in any links you want to poup and getElementsByTagName this is easy to achieve." ill try to find out how to create a rollover with css, though. pa 007: what do you mean you find it obtrusive? to open a new window? its only for a small player, that lets the user hear a preview of a tune. and i couldnt figure out another way to do this. thanks anyway. esther |
|
#7
|
|||
|
|||
|
Re: problem with main page when opening new window
Sorry, I should have given more of an example. I'm going to bed now but I'll post up some code in the morning. If you're clever about it you can create a function and that creates the popup behaviour then all you have to do is add a class to the link you want to 'pop up' (class="blah"). You just 'grab' that element/class using the DOM (Document Object Model). I'll post more in the morning but if you do it this way even when JavaScript is switched off everything still works, also your pages are nice and light (almost as if there is no JS) and you can reuse functions across your site (just like css).
Pete. Last edited by pa007; Jul 19th, 2007 at 01:40. Reason: cos it didn't make any sense |
|
#8
|
|||
|
|||
|
Re: problem with main page when opening new window
Opening a new window and I think JavaScript is the best way to do it. It just depends how you go about it. It we do all the JS in a different file and link it to the main page that means you aren't bloating your page with unnecessary code. It is considered inefficient and poor practice to use inline CSS so likewisae we shouldn't be using inline JavaScript. We don't even need inline event handers which is brilliant really. So using the same hooks that we use to apply CSS we can give some JS functionality. You will write these links as if they are normal none popup links.
We have these links:
Like so:
This bit of html will reference our external JS file:
Here is out JS code, I'll talk you through it in a moment:
You may not have understood a word of this but it can be a starting point. I would encourage you to learn about the DOM and unobtrusive JS. By unobtrusive we mean the sort of JS you have seen here. You take a page that works perfectly and alter it functionality. This way if JS is switched off then although some functionality may be lost everything still works. Try this out see if it works. This in combination with CSS rolovers will reduce you code massively as you won't need any inline code just a class or two. Any questions then just ask. Pete. |
|
#9
|
|||
|
|||
|
Re: problem with main page when opening new window
The css rollevrs are simple and though ti isn't JS I'll post it here since it may be of some use to you.
If it was just text you would use:
Image replacement:
The best method is to use a single image which has both states (link and hover) in it perhaps side by side. This way you can simply change the BG position on the a:hover. This will make the rollover seamless. I can give more info on this if you would like but I don't want to overload you. There a few tutorials knocking about on CSS Rollovers and BG position ones specifically. Pete. |
|
#10
|
|||
|
|||
|
Re: problem with main page when opening new window
oh WOW,
..... there is so much to learn...., its good to know there are people around who are willing to help so generousely. thanks esther |
|
#11
|
||||
|
||||
|
Re: problem with main page when opening new window
* all hail to Pete *
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#12
|
|||
|
|||
|
Re: problem with main page when opening new window
but, im still having some trouble. two things: 1. ive got a few different rollover buttons on the page. when i insert the css code for a hover button, all of them change to the same one, the listen button - of course.... how can i get them to appear only where they are supposed to be? and leave the rest of the buttons with different images? 2. when i click the listen link (a text button at the mo), i dont get a new window. it just opens in the same page like a normal link, although i created a new js file with the js code in it and linked it to the html page with this code: <script type="text/JavaScript" src="popUp.js"></script> |
|
#13
|
||||
|
||||
|
Re: problem with main page when opening new window
Is this online? It's easier to help when we can actually see it for ourselves...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#14
|
|||
|
|||
|
Re: problem with main page when opening new window
Did you add a class="popup" to you links?
And for the rollover. Use a class again. That way the css will only apply to elements with that combination of element and class. Post some code so we can take a look. I had the JS working before so its probably easy to fix. Pete. |
|
#15
|
|||
|
|||
|
Re: problem with main page when opening new window
ok, i tried it in a seperate file and got the js to work....!? tried it again in the page and it didnt work (link opens in the same page)...!?????
(my head section)
the code i put into a new test file, where the popup works properly
Last edited by Ice; Jul 20th, 2007 at 07:58. |
|
#16
|
|||
|
|||
|
Re: problem with main page when opening new window
Well I've had the JS working in page and if it works when its called from and external file then it should work when its in the html file. It could have been clashing with something or you may have called it incorrectly.
I never use styles withinh the page so I could be wrong but I think that those comments are your problem. The styles are within a comment so I don't think it will work. Remove <!-- and --> and see what happens. If you want to use the JS in your page (which you shouldn't) then post the code you used to link to the JS and I'll check it. Pete. |