I need to use javascript function in Menu Links (in fact in links within Bookmark module, based on Menu Links).
Through the "Add item" field, I cannot enter url like:
javascript:myfunction('myfile.php?myparam=value')
Indeed, I have the following error: "The path 'myfunction('myfile.php' is either invalid or you do not have access to it." and the link is not created.

If I go through PhpMyAdmin interface, I can use this javascript url in the "link_path" but then, in my page, the link becomes:
http://localhost/drupal-6.10/?q=javascript%3Amyfunction%28%27myfile.php...

So, the function is not called.
Is there a way to call javascript ?

Comments

jonline’s picture

I am also looking for this kind of stuff.

brian.sanchez’s picture

What are you trying to do?, please be more specific.

Look, to call a javascript function use:

drupal_add_js('path/to/file.js');

then in your javascript file use the "location" javascript function:

function myfunction(value'){
window.location='myfile.php?myparam='+value;
}

"Through the "Add item" field, I cannot enter url like:
javascript:myfunction('myfile.php?myparam=value')"

That's true, because every "Menu Item" should be declared, that's why I say, be more specific please.

Good luck!

pyctures’s picture

Thanks for your interest. I am not fully technical so could you enter in details ?

For the moment, I made that test:
I added a script in the "page.tpl.php" of my theme.
I also added a "popup.php" page.
In the Bookmark module page (bookmarks.module), I added an hardcoded link:
<a href="javascript:myfunction(\'popup.php?param1=value1&p2=v2&p3=v3\')">TEST</a>
This is working well: when I click on the TEST link, the script is called and executed.
=> the popup page appears.

Now, I want to use this kind of link (javascript:myfunction(\'popup.php?param1=value1&p2=v2&p3=v3\')) in the url fields of my Menu Items. Is it possible ? How to proceed ? Do I need to modify the Bookmarks Module ? The Menu Link (Core) module ? How ?

zbombicz’s picture

Same problem,

I want to add "Add bookmark" menuitem into primary menu (drupal core). The function is written in js, so the final html code should be in format like:

...
<li class="leaf">
<a href="javascript: addBookmark('var1','var2');" >Add bookmark</a>
</li>
...

Is there any solution to do this? Thanks

dustyfox’s picture

Although this post is pretty old it deserves a simple answer for future reference.

You can do a little something like this to add javascript call instead of link to your menu item:

edit template.php in your theme folder, locate

function yourthemename_menu_item_link($link)

if it doesn't exist just create it.

add an if-statement inside the function:

if ($link['title'] == 'Call javascript'){
    return '<a href="javascript:myJavaFunction();">'.$link['title'].'</a>';
}

Where 'Call javascript' is the title of your menu item. Obviously you can use any valid Path when you create the menu item such as as it will be overwritten by this function.

The easiest way to hack links is using their title in my opinion. Some might shrug at this but It works as it should, although not a great candidate for localizing menu labels. If you should need to localize this menu link (e.g. using Internationalize module) make it available only in one language and then duplicate the item with the localized title for your second language.

yogarenzi’s picture

Thanks, I've been trying to figure out how to do the same for a few days now. I followed your directions, but I'm getting a syntax error. What did I do wrong?

if ($secondary_links['title'] == 'my account'){
return ''.$link['title'].'';
}