Code from exercise doesn't work outside of codecademy


#1



Exercise 5. I tried using this code outside of codecademy however when i click the post button it doesn't post/clear the text from the 'status-box.' also wasn't updating counter but I have taken that out. I think it isn't linking to my javascript file correctly. Ty in advance to anyone who takes a look.


In my html:
 <div class="col-sm-3">
      <ul class="posts">
      </ul>
      <form>
        <div class="form-group">
          <textarea class="form-control status-box" rows="1" placeholder="What's on your mind?"></textarea>
        </div>
        </form>
        <div class="button-group">
          <a href="#" class="btn btn-primary">
          <img src="http://i.imgur.com/MCnN2wS.png" width="30" height="30">
          </a>
      </div>
      </div>
      </div>
    </section>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 	   <script src="app.js"></script>
    </body>
    </html> 

In my javascript:
var main = function() {
    $('.btn').click(function() {
    var post = $('.status-box').val();
    $('<li>').text(post).prependTo('.posts');
    $('.status-box').val('');
    $('.btn').addClass('disabled');
    })
    
    $('.btn').addClass('disabled');
}

$(document).ready(main);


#2

Hi Cioudyy,

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Change that line to:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

instead, and your code will work :slight_smile:


The reason it doesn't work locally right now is because // is what's called a protocol relative URL. This means that if you access your website over HTTPS, it will fetch the resource over HTTPS, and if you visit via HTTP, the resource will be delivered via HTTP. And if you visit your website with the file protocol (file://...), then it will try to fetch the resource via file. The problem is, the file protocol is for viewing local files stored on your computer only, you can't use it to fetch resources from a remote server. So you need to use HTTP(S) instead :slight_smile:

Protocol relative URLs were a good idea a few years ago, but should not be used anymore due to various reasons.


#3

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.