It is possible to do this with PowerShell. This does require the "Minimal Download Strategy" feature to be disabled on the site in question, otherwise you will receive an error when clicking on the link. Note, this might also be required for other links such as mailto: or file:// to work properly.
In the SharePoint Management Management shell you can do something similar to the following.
#Add a new link
$url = "http://sharepoint.domain.com"
$spWeb = Get-SPWeb -Identity "$($Url)" -ErrorAction SilentlyContinue
# Add link
# The format is DisplayName, link, is external (true/false)
$node = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("Test Link", "javascript:alert('test alert')", $true)
$spWeb.Navigation.QuickLaunch.AddAsFirst($node)
$spWeb.Dispose()
#Update existing link
$spWeb = Get-SPWeb -Identity "$($Url)" -ErrorAction SilentlyContinue
# Update link
# Select the menu if necessary
$selLink = $spWeb.Navigation.QuickLaunch | ? {$_.Title -eq "LinkName"}
$selLink.Url = "javascript:alert('test')"
$selLink.Update()
$spWeb.Dispose()
If you wanted to updated the Top Navigation, you can replace
$spWeb.Navigation.QuickLaunch with $spWeb.Navigation.TopNavigationBar