This solution should work effectively. Just ensure to add the src attribute after jQuery captures the viewport width and remember to update it whenever the window is resized.
<img id="img1" src="" class="MyImages">
<script type="text/javascript">
$(function() {
update_image();
$( window ).resize(function() {
update_image();
});
});
function update_image(){
var width = $( window ).width();
if(width<450) { var picwidth=100; } else { var picwidth=300; }
$('#img1').attr('src','timthumb.php?src=myimage.jpg&h=300w='+picwidth+'&q=100');
}
</script>
To address your query:
Modify your image elements as shown below:
<img src="" class="MyImages" data-src="myimage.jpg">
By using jQuery, you can now retrieve the correct sizes for your images:
<script type="text/javascript">
$(function() {
$('img').each(function(){
update_image(this);
});
$( window ).resize(function() {
$('img').each(function(){
update_image(this);
});
});
});
function update_image(e){
var width = $( window ).width();
if(width<450) { var picheight=100; } else { var picheight=300; }
var image = $(e).attr('data-src');
$(e).attr('src','timthumb.php?src='+image+'&h='+picheight+'&q=100');
}
</script>
Implementing this will ensure that all your images are dynamically updated when resizing the viewport.