views:

316

answers:

3

I have a dataTable in which one of the columns contains URLs. On click of those links I have to open respective link in a new page. I am using window.open() to open those links. It's opening a new window but not displaying the page. The javascript code I am using is:

function openDisclaimer(objectId)
{
var disclaimerID = objectId.id;
var url = document.getElementById(objectId).value;
window.open(url);
}

and

<h:commandLink id="dsclaimLink" value="#{managePersonalization.disclaimerURL}" onclick="openDisclaimer(this);" >
A: 

Just check what does objectID returns

Ravia
I am calling js on following:<h:commandLink id="dsclaimLink" value="#{managePersonalization.disclaimerURL}" onclick="openDisclaimer(this);" > Its giving ID for this http://localhost:9080/WEB/addPers.faces#
Isha Dubey
Its the Id of the cell of dataTable
Isha Dubey
what is the reason to down vote?
Ravia
A: 

Why don't you do it like this:

<h:commandLink id="dsclaimLink" value="#{managePersonalization.disclaimerURL}" 
    onclick="window.open('#{managePersonalization.disclaimerURL}');" />
Bozho
Thanks for ur reply.Its giving javascript error ") expected"
Isha Dubey
well, just add the single-quote. I've forgotten it.
Bozho
I tried with single quotes also. Giving javascript error
Isha Dubey
what does the generated javascript look like?
Bozho
.In Mozilla, its giving error "c is not registered protocol"
Isha Dubey
then your URL is wrong. That's why me and BalusC want the generated javascript. Go to View Source and see.
Bozho
A: 

First, in ancient JSF versions there was a bug which caused that commandlinks can't work that way. Ensure that you're using the latest version/build. Mojarra is available here and MyFaces is available here.

As to your actual problem, a commandlink basically generates a link which submits the parent form. It's supposed to be placed inside a h:form, but here you just want a plain vanilla link. In this case, replace it by h:outputLink. Don't forget to add return false; to end of click event to block the link's default action.

<h:outputLink 
    value="#{managePersonalization.disclaimerURL}" 
    onclick="openDisclaimer(this); return false;">
    <h:outputText value="Disclaimer" />
</h:outputLink>

Further, the value attribute actually ends up as href attribute of the generated <a> element, so your JavaScript needs to be fixed that it gets the URL from href attribute rather than the value attribute (JavaScript doesn't see the JSF source code, but only its generated HTML output!):

function openDisclaimer(link) {
    window.open(link.href);
}

You also see that document.getElementById is removed because it makes no sense as you already have the sole element as function argument! ;)

You can even get rid of the whole function:

<h:outputLink 
    value="#{managePersonalization.disclaimerURL}" 
    onclick="window.open(this.href); return false;">
    <h:outputText value="Disclaimer" />
</h:outputLink>

Alternatively, if you actually want a worthfully tab/window instead of a popup window, then you can also just make use of the target attribute which you set to _blank:

<h:outputLink 
    value="#{managePersonalization.disclaimerURL}" 
    target="_blank">
    <h:outputText value="Disclaimer" />
</h:outputLink>
BalusC
Isha Dubey
I tried it with h:commandLink also. It is opening the same page inspite of link.
Isha Dubey
It's `h:outputLink`, **output** Link. Did you add `return false;`? Did you specify a **correct** URL? Do you have the recent JSF version? Is the `onclick` attribute generated in HTML output? Did you try the `target` attribute? Popups triggered by `window.open` are often blocked in modern browsers. Update your question with the the generated HTML output if you want better assistance.
BalusC
thanks. m using h:outputLink. JSF vrn. is 1.1
Isha Dubey
I added return false; url is correct . target attrbute tried.
Isha Dubey
1) Upgrade to 1.1_02 https://javaserverfaces.dev.java.net/files/documents/1866/33714/jsf-1_1_02.zip 2) Please update question with generated HTML output if you want better assistance (after upgrading to 1.1_02 and testing with it!).
BalusC
This doesn't represent a `h:outputLink` and the onclick is missing. You're likely using `t:inputFileUpload` or something similar. When posting JSF problems, please post the **real** components used and don't obfuscate the original/3rdparty used components with other or standard components.
BalusC
See, I have uploaded the link first using t:inputFileUpload and added to the datatable. The next step i need is i want the link to get opened in a new page. so what source should i send to make it clear
Isha Dubey
How to make the thing clear to you... As Bozho said it may be some url problem... html code when i click on the link is<a href="#" onclick="window.open('file://C:/html/a.html');;return oamSubmitForm('form1','form1:details:0:dsclaimLink');" id="form1:details:0:dsclaimLink">file://C:/html/a.html</a>
Isha Dubey
C:/html/a.html is the link i have added to the datatable and form1:details:0:dsclaimLink" is the id for the h:commandLink
Isha Dubey
Getting following error in mozilla Security Error: Content at http://localhost:9080/WEB/addPers.faces may not load or link to file:///C:/html/a.html.
Isha Dubey
Don't use `file://` links. Use `http://` links. Create a FileServlet if necessary: http://balusc.blogspot.com/2007/07/fileservlet.html
BalusC