HighDots Forums  

Use a 'variable' to avoid repeatedly typing URLs?

HTML Writing HTML for the Web (comp.infosystems.www.authoring.html)


Discuss Use a 'variable' to avoid repeatedly typing URLs? in the HTML forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John U.
 
Posts: n/a

Default Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 07:55 AM






Hi Group,

Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain (e.g., "http://
www.x12345.com/").

This is a bother because (1) it takes a lot of typing, and (2) more
importantly, if (or when) the main URL changes, it takes a lot of work
to update the links.

Is there an easy way to specify somewhere in my html a
"variable" (e.g. "$urlname"), and then assign it a value, so that
later in the page I can refer to this variable instead of the full
URL?

Thanks in advance.

John Uebersax

Reply With Quote
  #2  
Old   
Harlan Messinger
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 08:46 AM






John U. wrote:
Quote:
Hi Group,

Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain (e.g., "http://
www.x12345.com/").

This is a bother because (1) it takes a lot of typing, and (2) more
importantly, if (or when) the main URL changes, it takes a lot of work
to update the links.
Any text editor--even Windows Notepad--has a search-and-replace feature
that will change the domain for you in a single operation.

Quote:
Is there an easy way to specify somewhere in my html a
"variable" (e.g. "$urlname"), and then assign it a value, so that
later in the page I can refer to this variable instead of the full
URL?
Yes, if you're using *any* dynamic server-side Web technology (ASP.NET,
PHP, etc.), then you just set a variable and stick

<%=domainName%>

for example, wherever you need to.

I'm assuming the domain you're talking about is different from the one
your page is on. If it's all the same website, then you simply don't use
the http://domain.name part at all.


Reply With Quote
  #3  
Old   
Jukka K. Korpela
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 08:46 AM



Scripsit John U.:

Quote:
Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain
You can set the base URL, e.g.

<base href="http://www.x12345.com/">

(in the <head> part), but then it applies to _all_ relative URLs on the
page, including all href and src attributes. So _any_ URL attribute
value that does not being with a protocol part will be interpreted as
relative to the base URL defined that way. For example,
href="foo/bar.html" will mean the same as
href="http://www.x12345.com/foo/bar.html".

Quote:
Is there an easy way to specify somewhere in my html a
"variable" (e.g. "$urlname"), and then assign it a value, so that
later in the page I can refer to this variable instead of the full
URL?
No, there is no way in HTML. (Well, technically there is, namely
entities, but entity definitions aren't supported by any browser, except
perhaps in XHTML mode, which in turn doesn't work in IE.)

You might be able to use some preprocessing or server-side software,
such as PHP, for the purpose.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/



Reply With Quote
  #4  
Old   
Jonathan N. Little
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 01:01 PM



John U. wrote:
Quote:
Hi Group,

Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain (e.g., "http://
www.x12345.com/").

This is a bother because (1) it takes a lot of typing, and (2) more
importantly, if (or when) the main URL changes, it takes a lot of work
to update the links.

Is there an easy way to specify somewhere in my html a
"variable" (e.g. "$urlname"), and then assign it a value, so that
later in the page I can refer to this variable instead of the full
URL?

Thanks in advance.

Not in HTML but in a server-side script the answer is yes.

PHP:

$bigArrayOfPagesObjects;
$base='http://www.example.com/";

foreach($bigArrayOfPagesObjects as $object){
echo '<li><a href="' . $base . $object->href . '">" .
$object->text . '</a></li>';
}


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com


Reply With Quote
  #5  
Old   
maya
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 01:22 PM



Jonathan N. Little wrote:
Quote:
John U. wrote:
Hi Group,

Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain (e.g., "http://
www.x12345.com/").

This is a bother because (1) it takes a lot of typing, and (2) more
importantly, if (or when) the main URL changes, it takes a lot of work
to update the links.

Is there an easy way to specify somewhere in my html a
"variable" (e.g. "$urlname"), and then assign it a value, so that
later in the page I can refer to this variable instead of the full
URL?

Thanks in advance.


Not in HTML but in a server-side script the answer is yes.

PHP:

$bigArrayOfPagesObjects;
$base='http://www.example.com/";

foreach($bigArrayOfPagesObjects as $object){
echo '<li><a href="' . $base . $object->href . '">" .
$object->text . '</a></li>';
}


how about doing it with JavaScript???

var url = "http://www.mydomain.com/";


would have to generate all links with document.write() lines,

document.write('<a href="' + url + 'contact.html">contact</a><br>');

(don't remember if you have to 'escape' double-quotes in JavaScript, if
you do it would be

document.write('<a href=\"' + url + 'contact.html\">contact</a><br>');

in which case it could also be

document.write("<a href=\"" + url + "contact.html\">contact</a><br>");


but I think this is better than shtml option mentioned earlier, and,
once you've coded all the links, it should be a breeze to change the
domain later if you have to, as you would have to just change it in the
variable...

I hope my JavaScript code is correct... consult in
comp.lang.javascript if you need to..

(btw, if you don't do any of this, and decide to hard-code entire url
for every link after all, changing it by using any HTML editor's
search-and-replace feature should not be too hard...







Reply With Quote
  #6  
Old   
maya
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 02:29 PM



Beauregard T. Shagnasty wrote:
Quote:
maya wrote:

how about doing it with JavaScript???

What about the ~10% of visitors who have it disabled, or stripped by a
corporate firewall?

people here and elsewhere are always saying this, but the fact is that
most websites out there (except maybe strict amateurs) have some kind of
JavaScript, I think if you disable JavaScript most websites wouldn't
function properly.. but oh well... it was just a suggestion...



Reply With Quote
  #7  
Old   
Michael Fesser
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 03:22 PM



..oO(maya)

Quote:
Beauregard T. Shagnasty wrote:
maya wrote:

how about doing it with JavaScript???
It's a big mistake to rely on JS to make a site work.

Quote:
What about the ~10% of visitors who have it disabled, or stripped by a
corporate firewall?
Or search engines.

Quote:
people here and elsewhere are always saying this, but the fact is that
most websites out there (except maybe strict amateurs) have some kind of
JavaScript
Which is not bad if done correctly. I use it too.

Quote:
, I think if you disable JavaScript most websites wouldn't
function properly.
Then it was done incorrectly. Relying on JS is not only bad for the
visitors that have it disabled or not available at all. It can also
become a problem for the site author himself, because search engine bots
are not capable of executing JS as well. Of course it doesn't matter if
the author doesn't want his site to be found and indexed properly.

Good websites also work with JS disabled. There's always an alternative
way for doing such things.

Micha


Reply With Quote
  #8  
Old   
Blinky the Shark
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-14-2007 , 05:37 PM



maya wrote:

Quote:
Beauregard T. Shagnasty wrote:
maya wrote:

how about doing it with JavaScript???

What about the ~10% of visitors who have it disabled, or stripped by a
corporate firewall?


people here and elsewhere are always saying this, but the fact is that
most websites out there (except maybe strict amateurs) have some kind of
JavaScript, I think if you disable JavaScript most websites wouldn't
function properly.. but oh well... it was just a suggestion...
I use NoScript. It's not very often that I have to enable scripting for a
site to work.

--
Blinky
Killing all posts from Google Groups
The Usenet Improvement Project - http://improve-usenet.org



Reply With Quote
  #9  
Old   
Scott Bryce
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-15-2007 , 02:00 AM



maya wrote:
Quote:
the fact is that most websites out there (except maybe strict
amateurs) have some kind of JavaScript,
I use very little JavaScript, and I am not a strict amateur.

Quote:
I think if you disable JavaScript most websites wouldn't function
properly.
I'd say that's a pretty good reason not to use JavaScript.


Reply With Quote
  #10  
Old   
Steve Swift
 
Posts: n/a

Default Re: Use a 'variable' to avoid repeatedly typing URLs? - 12-15-2007 , 03:25 AM



John U. wrote:
Quote:
Here is a common problem: I have many (> 100) links on a web page,
all pointing to different pages on the same domain (e.g., "http://
www.x12345.com/").
If they are in the same domain as the page itself then use relative
URL's (it's not 100% clear if the page itself is in the same "same
domain"; I suspect not though)

Examples: (all referring to pages on the same server)
HREF=page.html (in the same directory as the current page)
HREF=subdir/somepage.html (in subdirectory subdir of the current)
HREF=/abs_dir/subdir/stuff.html (same server; absolute path)

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.