PDA

View Full Version : How can I call a javascript function from a link on popit menu?



patelekta
05-25-2011, 03:13 PM
1) Script Title: Pop-it menu
2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex1/popit.htm

3) Describe problem:I have a java script function which allow me to open a popup window as given below,

function openPopup(url, name, w, h) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl;
win = window.open(mypage, myname, winprops)
}

I need to call this function from a link on pop-it menu, my code to call the java script function from the link on pop-it menu as given below


$linkset[0] .="<a href=www.google.com> Show Linked Companies </a>";


$linkset[0] .="<a href=javascript:openPopup('company.php?Action=ShowLinkedCompany&companyId={$companyId}', 'LinkedCompany', 900, 600);> Show Linked Companies </a>";

But with this type of link pop-it menu is not being generated.
Is their any way I can do this?

jscheuer1
05-26-2011, 02:11 PM
$linkset[0] .= '<a href="company.php?Action=ShowLinkedCompany&companyId={$companyId}" target="LinkedCompany" onclick="openPopup(this.href, this.target, 900, 600); return false;"> Show Linked Companies </a>';

One thing I'm not clear on though, what's:


{$companyId}

Is that some sort of token that gets replaced server side? If so, and it does get replaced, it should work out.

patelekta
05-26-2011, 04:49 PM
Thanks a lot Jhon, for your reply...

I did in different way for now, but in future i will definitely use your way so that I don't have to create multiple java script function to open a popup. I did it right now as given below

1 - Create a new java script function as shown
function PopupLinkCompany(compId){
var left = (screen.width/2)-(800/2);
var top = (screen.height/2)-(400/2);
winprops = 'height=400,width=800,top='+top+',left='+left;
url = 'company.php?Action=ShowLinkedCompany&companyId='+compId;

var Win = window.open(url, 'LinkCompany', winprops );
}

2 - Change the link in pop-it as given below
$linkset[0] .="<a href=javascript:PopupLinkCompany($companyId);> Show Linked Companies </a>";

I notice that, I can call the JavaScript function with a single input parameter and the input parameter should not be in single quote.

Anyway, thanks again for your reply...

jscheuer1
05-26-2011, 05:13 PM
I notice that, I can call the JavaScript function with a single input parameter and the input parameter should not be in single quote.

As in all coding languages, when there are nested quotes you only have the two types to work with single and double. Once your requirements go beyond that you have to either write it differently or start escaping one or more levels of the quotes.

I can't see what $companyId is getting resolved to. But if it is a value encased in quotes, single quotes - as it pretty much would have to be in the code you are showing - assuming that code is literal, then you've just used up the two types of quotes. Anything around $companyId cannot also use single quotes unless they're escaped for $companyId.

It's easier to figure out if one looks at the live page in the browser and uses the browser's 'view source'. Then one can see how the server side tokens are being resolved and if anything needs to be escaped - assuming one knows proper quoting syntax for javascript.