0

I need to create on snippet of javascript that, based on an A tags id, the window will navigate to the proper html file. I've got something together that when I look at it, it should work but for some reason it doesnt. Here's what I got.

<script> 
    $(document).bind('pageinit', function() { 
        $('a').each(function (index){
            var elementId = $(this).attr("id"); 
            elementId= elementId + '.html';
            $(function(){ 
                $(elementId).click(function (event) { 
                    event.preventDefault(); 
                    window.location.assign(elementId); 
                }); 
            }); 
        });
    }); 
</script>

This part is so that I can load an external html in an ios web app with out exiting the web app window

$(function(){ $(elementId).click(function (event) { 
    event.preventDefault(); 
    window.location.assign(elementId); 

Did I write the variable wrong some how? Any help would be appreciated

3
  • 1
    Could you fix the indentation in your code sample? It's hard to tell what's really going on when it's formatted like this. Commented Feb 13, 2013 at 17:19
  • Now what is that DOM ready function doing in there ? Commented Feb 13, 2013 at 17:21
  • The DOM ready function is there to run the code when my first page is loaded. It may not be the proper way to do it, but I have a variation of this where there is no variable and it works fine. Commented Feb 13, 2013 at 17:24

2 Answers 2

2

I'll take a wild guess:

$(function(){ 
    $('a').on('click', function(e) {
        e.preventDefault();
        window.location.assign(this.id + '.html'); 
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Just to be clear, this would replace all of his script and not just the inner portion he highlighted, correct?
Awesome, it works. Only had to change the 'a.html' to 'a' but it works. Thanks alot
1

This is a simplified version of what you have there...

<script>
    $(function() {
        $("a").on("click", function(e) {
            e.preventDefault();
            window.location.href = this.id + ".html";
        });
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.