/    Sign up×
Community /Pin to ProfileBookmark

Changing link based on variable

I need to add a value to a link, based on the value of a variable. (This is because my site is not allowed to use cookies.) I am passing this variable from one page to another by means of a function:
<script language=”JavaScript”><!–
//this function will take a passed URL and add the ?db= segment.
//Do NOT call this for pages going to the correct Basic/Advanced pages.
function intercept(what) {
what.href += ‘?db=’ + db; //adds the correct db scenario (?db=…) to the end of the URL
}
//–></script>

However, there are a few times where I need to add this to the URL itself–instead of basic/basic.htm I want the user to go to basic/basic_”valueofdb”.htm.

I can’t figure out how to do this! I can write code that will put the correct path into a variable, but cannot return this variable to the link.

ANy advice?

N

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreFeb 24.2003 — There are a few solutions to this:

Javascript: would work dynamically (i.e. no refresh needed), but would fail on over 10% of users views.

Iframe/div etc: Would work on most browsers, no refresh. However, the iframe etc. would have to refresh, not all browsers support them, and would require many pages, just with links in.

PHP, ASP etc: Would work on all browsers, require refresh, not all servers support it. If your servers support PHP or ASP, these are the most compatible.

So, which do you want to use? Here's a summary:

Fastest: JavaScript

Least Bandwidth: JavaScript

Most Compatible for users: PHP
Copy linkTweet thisAlerts:
@nluomaauthorFeb 24.2003 — The only option available to me due to constraints on the system is JavaScript. The 10% failure rate is not a problem, because I am writing a help system for an online service that *requires* users to have JavaScript enabled.

I've been playing around with the code and came up with the following:

Here is the call:

<a href="folder/file.htm" onClick="basicintercept(this)">Sample Link</a>

The code I then use to change folder/file.htm to incorporate the value "abc" (stored in the variable db) to folder/file_abc.htm?db=abc is as follows:

function basicintercept(what) {

path = what.pathname;

path=path.split('.htm');

path[0] = unescape(path[0]) + '_' + db + '.htm?db=' + db; //adds the correct db scenario (?db=...) to the end of the URL

what.pathname=path[0];

}

This seems to work, but is there a better, more compact way to do this?

Thanks!
×

Success!

Help @nluoma spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...