Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In drupal_get_js's current state, we can't have inline scripts appearing above external scripts. This is important for many external third party web services where they expect local variables to be initiated before using their external script....
drupal_add_js('/* This should appear above the external script! */', array(
'type' => 'inline',
'scope' => 'footer',
'weight' => JS_THEME - 10,
));
drupal_add_js('http://example.com/example.js', array(
'type' => 'external',
'scope' => 'footer',
'weight' => JS_THEME + 10,
));
Expected output is......
< script type="text/javascript">/* This should appear above the external script! */</script>
< script type="text/javascript" src="http://example.com/example.js"></script>
....But it results in the reverse order.
< script type="text/javascript" src="http://example.com/example.js"></script>
< script type="text/javascript">/* This should appear above the external script! */</script>
Comment | File | Size | Author |
---|---|---|---|
#11 | 746676.patch | 2.1 KB | RobLoach |
#5 | 746676-inline-js-by-weight-5.patch | 567 bytes | pwolanin |
Comments
Comment #1
RobLoachUhhhh, we expect......
<script type="text/javascript">/* This should appear above the external script! */</script>
<script type="text/javascript" src="http://example.com/example.js"></script>
....But it results in the reverse order.
<script type="text/javascript" src="http://example.com/example.js"></script>
<script type="text/javascript">/* This should appear above the external script! */</script>
This was as a result from #721400: Order JS files according to weight, don't change filenames for aggregated JS/CSS.
Comment #2
pwolanin CreditAttribution: pwolanin commentedSo I thought we did not want to mix inline with the rest, but guess I was wrong - this should be an easy fix, I think.
Comment #3
mfer CreditAttribution: mfer commented@pwolanin - The way it used to work was that inline and non-preprocess (external and internal) were intermixed by weight. It used to not mix preprocessed and non-preprocessed.
This makes me wish this was all documented well.
Comment #4
mfer CreditAttribution: mfer commentedSince this messes with JavaScript dependencies I'd call it critical.
Comment #5
pwolanin CreditAttribution: pwolanin commentedI think the fix here is 1-line.
Comment #7
pwolanin CreditAttribution: pwolanin commented#5: 746676-inline-js-by-weight-5.patch queued for re-testing.
Comment #9
pwolanin CreditAttribution: pwolanin commentedThis is a different fail than last time - feel like testbot is a game of roulette now...
Comment #10
pwolanin CreditAttribution: pwolanin commented#5: 746676-inline-js-by-weight-5.patch queued for re-testing.
Comment #11
RobLoachDoes botman like the test?
Comment #13
RobLoachIgnore the testing bot. The new JavaScript test with the mixed external/inline tags passed. The weighting is retained. Nice one, Peter ;-) .... I posted a follow up: #748794: Aggregate side-by-side inline JavaScript.
Comment #14
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.