I want to convert the following PHP code into JavaScript.
<body>
<?php
$opts = array('http'=>array('method'=>"GET",
'header'=>"User-agent: CUSTOM_USER_AGENT\r\n"));
$context = stream_context_create($opts);
$file = file_get_contents("http://example.com/file1", false, $context);
///open http://example.com/file1 with CUSTOM_USER_AGENT
$pattern = "/^Location:\s*(.*)$/i";
$location_headers = preg_grep($pattern, $http_response_header);
//find url where this redirects to
echo $location_headers
?>
</body>