954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding a Javascript Variable to a URL

Hey guys,

I have a simple problem in javascript that I can't figure out.

I am adding a feedburner url into an html page, but need to make it so you can increase the amount of posts you want to see. Ex. The default amount of posts on the page starts at three, but then I would have a button called more where you would click it to view say 3 more, and then if you clicked it again 3 more, ect.

This button would have to call a javascript function correct?

Right now I have:
(I know this is wrong, but it will give you an idea of what i am trying to do)

<script type="text/javascript">
var i=3;
function more()
{
var i = i + 3;
}
</script>
<script "text/javascript" >
document.write('<a href = "http://feeds.feedburner.com/domain/read?format=sigpro&nItems=' + i + '">');
        </script>
        <noscript>
        
        <p>Subscribe to RSS headline updates from: <a href="http://feeds.feedburner.com/domain/read"></a><br/>Powered by FeedBurner</p> 
        </noscript>		
		<form>
<input type="button" value="More" onclick="more()" />
</form>


The nItems declares how many posts are on the page, so I would want it to equal a variable that can be changed via the button. Also if someone knows how to add ajax so you don't have to refresh the page, that would be a nice extra.

Thanks! I look forward to your responses.

TheVendor
Newbie Poster
19 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You could use PHP or another server side scripting language to print that value to javascript:

<script type="text/javascript">
var i = <?php echo $_GET['nItems']; ?>;
...
</script>
I know this is wrong


I don't know what you know is wrong, but realize that more() should contain a window.location change, or you should write something like '...&nItems=' + (i + 3) + '">'.

Also, think hard if you really need javascript for this. You could just as well write (php):

<a href="http://feeds.feedburner.com/domain/read?format=sigpro&nItems=<?php if(!$_GET['nItems']) $_GET['nItems'] = 3; echo $_GET['nItems'] + 3; ?>">more</a>
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

Thanks for your response.

Problem is, I can't use PHP because this is for a BlackBerry Playbook app and the PHP runtime does not exist on the device. I have to package the file with only HTML, CSS and JAVASCRIPT files.

A friend took a shot at this, but the code still doesn't seem to be working. Here is the complete code to test functionality:

<html>
<head>
    <style>
        /* Resyndicator
    ----------------------------------------------- */
    
    div.feedburnerFeedBlock p.date {
        color: #666;
        }
    div.feedburnerFeedBlock ul li div {
        display: inline;
        }
    div.feedburnerFeedBlock span.headline a {
        font-weight: normal;
        font-size: 1.4em;
        color: #F33;
        text-decoration:none;
        }
    div.feedburnerFeedBlock ul li {
        margin-bottom: 2em;
        padding-left: 0;
        text-indent: 0;
        }
    div.feedburnerFeedBlock ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    </style>
<script type="text/javascript">
function more()
{
var i = i + 3;
document.getElementById('feedburner').src =
"http://feeds.feedburner.com/sofarsofresh/read?format=sigpro&nItems="+i;
}
</script>
</head>

<body>
    <h2>Posts</h2>
<script id="feedburner"
src="http://feeds.feedburner.com/sofarsofresh/read?format=sigpro&nItems=6" type="text/javascript"></script>
<noscript>
<p>Subscribe to RSS headline updates from: <a
href="http://feeds.feedburner.com/sofarsofresh/read"></a><br/>Powered
by FeedBurner</p>
</noscript>
        <form>
<input type="submit" value="More" onClick="more()" />
</form>

</body>
</html>
TheVendor
Newbie Poster
19 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You need to declare i outside of more(): var i=6; function more() { i += 3; ... }

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

That didn't seem to fix the issue :/

TheVendor
Newbie Poster
19 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: