Jump to content

Recommended Posts

Don't know if this is the place for this, so move it if it is necessary.

 

How do you force a link to open in a new window while still being XHTML compliant (I'm using Transitional) and not using JavaScript? I can't find documentation that says using "target" is valid.

 

I'd like to put that information inside <noscript> tags because I can do it with JS, I just would like to know how to, if it is possible, without JS.

Link to comment
https://forums.phpfreaks.com/topic/51746-solved-open-new-window/
Share on other sites

Becasue it is 'invalid'!!!!!!

 

In terms of standards compliance opening a new window is considered a document effect and as such shoudl be controlled by javascript.

 

Here is a bit of code that will help in this respect in an accessible mode.

 

first the html...

<a class="newwindow" href="/path/to/page" title="Link Title" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Link Text</a>

 

a bit of css. // this hides the text if js is on.

a.newwindow span
{
position: relative;
left: -5000px;
width: 0;
height: 0;
overflow: auto;
/* may need display block or a float to ensure no space is taken up when rendering */
}

 

and finally a bit of js to add the 'opens in new window text' this only appears if JS is on.

 

function newwindowA()
{
var as	=	document.getElementsByTagName('a');
for(var i=0;i<as.length;i++)
	if	(as[i].className == 'newwindow')
	{
		as[i].innerHTML 		+= ' <span class="hidden">(new window)</span>';
		as[i].style.padding		= '0px 17px 0px 0px;';
		as[i].style.background	= background: transparent url(/global/images/new-window-icon.gif) no-repeat right center;';		}
return;
}

 

If js is off then the page shoudl open in teh same window nad neither the newwidow icon of the text should show.

 

If js is on then the opens in newwindow text is shown and if css is on the txt will be hidden and the icon shown...

 

This is an alteration to some code I have so the style.background may require separate lines of code for each value..

 

[attachment deleted by admin]

well....

 

If you use target="_blank" browsers will still open a new window - they don't implement as strictly as they should, but in terms of standards compliance document effects should be js controlled.  Browsers (IMO) should apply the standards to the recognized doctypes so in that sense xhtml 1.1 should never open a new window without js.

 

Before others have their two penneth - remember we can CHOOSE which dtd we use - if we CHOOSE to use a strict one we should stick to it - that is what the web standards project is trying to help achieve...

The only problem with using JavaScript is that ability user have in turning it off. I'm of the opinion that if they're stupid enough to turn something as well used JavaScript off then they deserve broken pages. But this problem isn't the best thinking if you're getting paid for your work.

I validated a page using the target attribute under transitional (and no I didn't switch to it, lol) and it didn't generate any errors.

 

So would you recommend doing something like this?

 

<script type="text/javascript">
document.write("<a href=\"javascript:window.open('info.html');\" title=\"view detailed info\">more info</a>");
</script>
<noscript><a href="info.html" target="_blank" title="view detailed info">more info</a></noscript>

if you are validation using a transitional dtd then why not just leave the target="_blank"?

 

if you are developing to the standards and using someting like xhtml 1.1 (Personally I use that all the time now) then what I originally posted will do what you need.

 

document.write is not considered acceptable though - validators don't pick it up but you should use document.getElementById('element') as the object identifier and then set it properties...

 

e.g.

document.getElementById('element').innerHTML

@toon: I decided to switch my DOCTYPE to XHTML 1.1 and boy was that interesting. Anyway, moving on. After I got rid of all the errors, I tried to go back and use your JS example code from earlier down the thread. I don't quite understand something though.

 

If JS is off, how does the new page open? Will it just use the current page or will it actually open up a new window?

 

Earlier thread stuff so you don't have to scroll so much:

Becasue it is 'invalid'!!!!!!

 

In terms of standards compliance opening a new window is considered a document effect and as such shoudl be controlled by javascript.

 

Here is a bit of code that will help in this respect in an accessible mode.

 

first the html...

<a class="newwindow" href="/path/to/page" title="Link Title" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Link Text</a>

 

a bit of css. // this hides the text if js is on.

a.newwindow span
{
position: relative;
left: -5000px;
width: 0;
height: 0;
overflow: auto;
/* may need display block or a float to ensure no space is taken up when rendering */
}

 

and finally a bit of js to add the 'opens in new window text' this only appears if JS is on.

 

function newwindowA()
{
var as	=	document.getElementsByTagName('a');
for(var i=0;i<as.length;i++)
	if	(as[i].className == 'newwindow')
	{
		as[i].innerHTML 		+= ' <span class="hidden">(new window)</span>';
		as[i].style.padding		= '0px 17px 0px 0px;';
		as[i].style.background	= background: transparent url(/global/images/new-window-icon.gif) no-repeat right center;';		}
return;
}

 

If js is off then the page shoudl open in teh same window nad neither the newwidow icon of the text should show.

 

If js is on then the opens in newwindow text is shown and if css is on the txt will be hidden and the icon shown...

 

This is an alteration to some code I have so the style.background may require separate lines of code for each value..

If js is off then the page will open in the current window - just as the standards say it should...

 

the js is to both open the window and let the user know that the link will open a new window. if js os off then the user will not be told that a new widnow is about to open nor will a new window open.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.