string to href? [rookie]

Discussion in 'Javascript' started by cerr, Sep 2, 2011.

  1. cerr

    cerr Guest

    Hi There,

    I got a link as follows:
    <a href="">xyz</a>
    and a javascript function gethtml('es') that returns the url string.
    Now how do i get the url string into the html link in
    <a href="MyString">?

    Thank you!
    cerr, Sep 2, 2011
    1. Advertisements

  2. Where is that link, is it part of the HTML document inside the browser
    that you want to manipulate by script?
    Well if you have the link element object you can simply set
    link.href = gethtml('es');
    You can find all links in the
    collection so if you want to find a particular link element based on the
    existing href attribute value you could loop through document.links.
    Martin Honnen, Sep 2, 2011
    1. Advertisements

  3. cerr

    Tim Streater Guest

    What's 'es' got to do with anything? And I would post from google
    groups, I expect most people are filtering that out at present.
    Tim Streater, Sep 2, 2011
  4. cerr

    cerr Guest

    it is a regular link in the html document. What i want to do is:
    I have a complete page in english and i'll add multiple languages. The
    design has one design template that will always be the same for all
    the pages. on top i got buttons linking to different languages, my
    function reads out the address bar content, manipulates the url (by
    adding the language to this particular page) so that the link would
    link to this page in a different langfuage... does that make sense?
    So I just want to do document.write(gethtml('es')); for example.

    cerr, Sep 2, 2011
  5. Does the function gethtml already exist? Then post its code so that we
    can see what it does.
    If you want to use document.write then do that but it does not help to
    manipulate existing links, you can only write new ones or additional
    ones that way.

    However relying on client-side scripting to get links to work properly
    does not seem like a good idea.
    Martin Honnen, Sep 2, 2011
  6. cerr

    cerr Guest

    function gethtml(lang){
    var url = window.location.href;
    url = url.replace("","
    return url;
    I don't want to manipulate it but just to write it.
    True too but i still would like to try it this way...

    Thanks for your help!
    cerr, Sep 2, 2011
  7. cerr

    cerr Guest

    I do post from google groups and why would people filter it out
    anyways? :eek:

    'es' is just the language it should switch to - spanish in this

    function gethtml(lang){
    var url = window.location.href;
    url = url.replace("","
    return url;
    cerr, Sep 2, 2011
  8. cerr

    Tim Streater Guest

    Shitloads of spam.
    Tim Streater, Sep 2, 2011
  9. cerr

    cerr Guest

    How do I un-spam this then?
    cerr, Sep 2, 2011

  10. Because an ungodly amount of spam is comming through Google Groups,
    with the current batch having subjects of the form "<Famous person>
    nude", and their filtering isn't impressive (It'd probably be more
    readable if forwarded through gmail, who actually does block spam

    I don't see as much medicine spam these days, so maybe they found
    a way to recognize that.
    Lasse Reichstein Nielsen, Sep 3, 2011
  11. cerr

    dhtml Guest

    Just use a link; don't use js for that. E.g.
    <a href="/en_us/">American English</a>
    dhtml, Sep 8, 2011
  12. cerr

    dhtml Guest

    They got their viagra now they need some pornz.
    dhtml, Sep 8, 2011
  13. cerr

    dhtml Guest

    It's a bad idea. Can be quite annoying when it fails. It's as bad as
    using javascript to submit forms and if you have used the web at all,
    you know how annoying that can be. Here's a great example of that

    "I can't post a link to the comments that were lost (e.g. they weren't
    lost after being successfully added - they were never accepted/
    received by the system)"
    dhtml, Sep 8, 2011
  14. People seem to have had difficulties in understanding what you're doing,
    but having seen situations where such operations may make sense, I'm
    taking a guess: You have a page with local (relative) links, and you
    would like to rewrite some of those links so that they are replaced by
    absolute links with a part that selects a particular language. The
    specifics still aren't clear at all (a real URL would have helped a lot,
    as usual), and I doubt whether this is one of the rare cases where the
    approach makes sense, but... need some way of selecting the link(s) to be changed, and then
    you just do something like
    foo.href = gethtml('es') + foo.href
    where foo is a variable referring to an element.

    Oh wait... you have written "", which _is_ a relative link, but
    maybe you actually meant "", referring to an external
    site. Then I don't see why your gethtml() takes the _current_ page URL
    (window.location.href) as the starting point.
    Jukka K. Korpela, Sep 8, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.