SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing the value in a href tag

    how can I stored the value within an a href tag for example my php page does this
    Code:
    echo "<li><a href=\"JavaScript:article('$id')\">".$row['title']."</a> </li>";
    which prints this code on my ajax page
    Code:
    <a href="JavaScript:article('40')">What is the difference between Archiving and Trashing an Article? </a>
    . I want to save the value within href so that I can use it later. Please note its ajax so location.href doesn't work.

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if I can store the entire link tag that wouldn't be an issue either. In fact that may be preferred.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Is that link intended to be saved as a bookmarklet? If not then get the JavaScript out of there and into a click event where it belongs.. The href should be used for where you want people without JavaScript to go when they click the link.

    Once you get the script out of the HTML and into a separate JavaScript file you will be able to more easily change it to work the way you want.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Is that link intended to be saved as a bookmarklet? If not then get the JavaScript out of there and into a click event where it belongs.. The href should be used for where you want people without JavaScript to go when they click the link.

    Once you get the script out of the HTML and into a separate JavaScript file you will be able to more easily change it to work the way you want.
    Yes i want to use that link as a bookmark but when u click on it because it is ajax i dont see that in the address bar so i need a next way to save that value

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    Yes i want to use that link as a bookmark but when u click on it because it is ajax i dont see that in the address bar so i need a next way to save that value
    Virtually all web browsers prevent links from automatically saving as a bookmark, so the standard practice now is to instruct the user to drag the link to their bookmarks/favorites location.

    For an example, see http://delicious.com/help/bookmarklets
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Virtually all web browsers prevent links from automatically saving as a bookmark, so the standard practice now is to instruct the user to drag the link to their bookmarks/favorites location.

    For an example, see http://delicious.com/help/bookmarklets
    Ok well I am not really trying to save this to the browser per say but to localStorage so that when the user visits this page again on there mobile phone the links deh favor can be accessed in its own bookmark section within the web app. Is that still not a preferred practice as I see no other way to save the users favorite articles. The drag and drop will not be favorable for all phones hence why I am asking this.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    Ok well I am not really trying to save this to the browser per say but to localStorage so that when the user visits this page again on there mobile phone the links deh favor can be accessed in its own bookmark section within the web app. Is that still not a preferred practice as I see no other way to save the users favorite articles.
    Local storage can be used, but it's available only with HTML5. What you could do is to check if local storage is available, and if it's not you could use cookies instead to store the info.

    Since your links contain javascript, you should serialize the data first, before saving it to either local storage or a cookie.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know what serialize means. Also even if i used cookies I still don't know how to save the link itself as it doesn't appear in the address bar since its ajax am using so i dont know how to access the link after i click on it

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    I dont know what serialize means.
    There might be quite a lot for you to learn here, but we'll give it a go.

    To serialize, means to create a text-string version of the code. That might not strictly be necessary, but it can be a useful safety measure.
    The JSON library provides a nice and useful way to serialize data.

    Quote Originally Posted by skelleex View Post
    Also even if i used cookies I still don't know how to save the link itself as it doesn't appear in the address bar since its ajax am using so i dont know how to access the link after i click on it
    What you can do us to attach an onclick event to the section in which the link is contained. That way any click event will bubble up to where the onclick event is captured, so that you can then work with the link that was clicked.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok will read up on the JSON it looks a lil more complicated than i thought however on the onclick what function can I use to capture the <a> tag that the onclick came from? As i said I think once I can get the link I would be much better of in storing that data

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    on the onclick what function can I use to capture the <a> tag that the onclick came from? As i said I think once I can get the link I would be much better of in storing that data
    That all depends on how your page is structured.

    As a simple example, imagine that we have a content section in to which is placed the links.

    Code html4strict:
    <div id="content">
        <a href="#part1">Part 1</a>
        <a href="#part2">Part 2</a>
    </div>

    With JavaScript you can monitor all onclick events that occur within the content element.

    Code javascript:
    document.getElementById('content').onclick = clickHandlerForLinks;

    and that clickHandlerForLinks function can obtain the link that was clicked on, with which you can do what you like with it.

    Code javascript:
    function clickHandlerForLinks(evt) {
        evt = evt || window.event;
        var targ = evt.target || evt.srcElement;
        if (targ.nodeType === 3) {
            targ = targ.parentNode;
        }
        if (targ.nodeType === 1 && targ.nodeName === 'A') {
           // we have a link, do what you will with it.
           doSomethingWith(targ);
        }
        return false;
    }

    The evt line get the event in a cross-browser compatible way, since Internet Explorer likes to do things differently from most other web browsers.

    The targ line obtains information from the event about what was clicked. Once again, Internet Explorer is the odd one out using srcElement whereas most other web browsers use target instead.

    Checking that the nodeType equals 3, is to deal with an Opera bug of setting the target to a text node, so taht we can get the target back to an element node instead.

    The if statement checks that we have an element node (nodeType of 1) and that the element node has a nodeName of "A".
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok that was a bit confusing am a beginner so bear with me I am to assume that the tag will be stored in the targ variable correct also m going to attach my code to this post to see if it makes sense to you. So my new_articles.html calls new_articles.php toobtain the list of articles new_articles.php echos the urls with <a> tags and all. When the user now click on the link it calls article_desc.php to display the article. so basically i want to save the url that calls article_desc.php when a user clicks a button to make it a favorite.
    Sorry if the code is scrappy but I haven't gotten around to cleaning it up yet
    Attached Files Attached Files

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    ok that was a bit confusing am a beginner so bear with me I am to assume that the tag will be stored in the targ variable correct also m going to attach my code to this post to see if it makes sense to you.
    I will certainly help where I can to help you understand what you need to know in order to achieve what you want to do, but I'm afraid that I won't do the actual work for you.

    I have to head off now, but if I have time later on I'll take a look over your code and see if there's anything helpful that I can come up with.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I will certainly help where I can to help you understand what you need to know in order to achieve what you want to do, but I'm afraid that I won't do the actual work for you.

    I have to head off now, but if I have time later on I'll take a look over your code and see if there's anything helpful that I can come up with.
    No no am not trying to do that am trying to see how the code you send will fit in in my instance your code you put earlier made reference to some html code and javascript but I don't understand it and where it applies. So i cannot begin to even tweek it to suit my needs. The reason i sent the code was to speak about it in my case not to make you my resident coder.

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    No no am not trying to do that am trying to see how the code you send will fit in in my instance your code you put earlier made reference to some html code and javascript but I don't understand it and where it applies. So i cannot begin to even tweek it to suit my needs. The reason i sent the code was to speak about it in my case not to make you my resident coder.
    The first thing that I see is that you're using Joomla for your framework. Very few of us here know much about Joomla, so I would get on to the Joomla forums to find out if Joomla contains anything that can help you to achieve what you require in a much easier way.

    Failing that, we can fall back on to some old-school JavaScript programming.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The first thing that I see is that you're using Joomla for your framework. Very few of us here know much about Joomla, so I would get on to the Joomla forums to find out if Joomla contains anything that can help you to achieve what you require in a much easier way.

    Failing that, we can fall back on to some old-school JavaScript programming.
    While I am using Joomla it is only the db so the Joomla framework will not help in this instance. Everything i built was from scratch. I tried the code you sent before and it pulls the link fine however I get an error when trying to use the link the Error is
    Forbidden You don't have permission to access /Weblog 5/JavaScript:article('39') on this server.
    Which is very strange. Now going to read up on JSON


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •